WebClient.UploadData

L

Lehel Kovach

I'm having a problem with the WebClient object. When I post data to certain
sites, it will receive a command from the webserver (object moved) and
continue to the next link by downloading that. The problem is, certain
cookies need to be set, and the only way of doing that is not letting the
WebClient automatically go on to the next GET in the background--I need to
do it manually.

I'm wondering if this has something to do with the Connection: Keep-Alive
header. If this is the case, how do I change it? Because, everytime I
tamper with this header, WebClient.UploadData doesn't work anymore.
 
D

Dave Sexton

Hi Lehel,

Try using this class instead of WebClient:

/// <summary><see cref="System.Net.WebClient" /> implementation
/// that does not honor http redirect response status codes.</summary>
public class HttpNonRedirectingWebClient : System.Net.WebClient
{
public HttpNonRedirectingWebClient()
{
}

protected override System.Net.WebRequest GetWebRequest(Uri address)
{
// Allow the base method to create the WebRequest and try to cast it to HttpWebRequest
System.Net.HttpWebRequest request = base.GetWebRequest(address) as System.Net.HttpWebRequest;

if (request == null)
// TODO: create resource string for exception message
throw new ArgumentException("HttpNonRedirectingWebClient requires the address argument to use the http protocol.",
"address");

// ** disable auto-redirection **
request.AllowAutoRedirect = false;

return request;
}
}
 
L

Lehel Kovach

Actually, I just figured that out. Thanks.

My real problem lies with the WebClient object and the
HttpWebRequest/HttpWebResponse object (with AutoRedirect); Apparently,
during autoredirect, the cookies aren't set each time a redirect command is
given by HTTP. Is this a problem with the .NET coding or is this something
you just manually have to do? IE does this correctly and I just want to
mimic how IE does it. Is this something that I have to manually code in or
is HttpWebRequest broken in this regard, where Microsoft has to fix it?
 
D

Dave Sexton

Hi Lehel,

I haven't tested that myself. I'm not sure whether it's by design or a bug, but I don't think it would be too difficult to code
anyway.

You can use my code that I posted to disable redirection. After a response is received check for the redirection status code (I
think it is 303 but I could be mistaken and I think there are more than just one) and grab the location header to which you must
send a new request.
 
J

Joerg Jooss

Thus wrote Lehel,
Actually, I just figured that out. Thanks.

My real problem lies with the WebClient object and the
HttpWebRequest/HttpWebResponse object (with AutoRedirect); Apparently,
during autoredirect, the cookies aren't set each time a redirect
command is given by HTTP. Is this a problem with the .NET coding or
is this something you just manually have to do?

WebClient doesn't track cookies by default (nor does HttpWebRequest). You
can use Dave's approach to enable cookies as well: Create a CookieContainer
object as an instance member of your class and assign this to each HttpWebRequest's
CookieContainer property.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top