By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,660 Members | 1,103 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,660 IT Pros & Developers. It's quick & easy.

WebClient.UploadData

P: n/a
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.
Aug 8 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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;
}
}

--
Dave Sexton

"Lehel Kovach" <le*********@hotmail.comwrote in message news:9Y*******************@tornado.socal.rr.com...
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.

Aug 9 '06 #2

P: n/a
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?

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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;
}
}

--
Dave Sexton

"Lehel Kovach" <le*********@hotmail.comwrote in message
news:9Y*******************@tornado.socal.rr.com...
>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.


Aug 9 '06 #3

P: n/a
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.

--
Dave Sexton

"Lehel Kovach" <le*********@hotmail.comwrote in message news:YG******************@tornado.socal.rr.com...
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?

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
>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;
}
}

--
Dave Sexton

"Lehel Kovach" <le*********@hotmail.comwrote in message news:9Y*******************@tornado.socal.rr.com...
>>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.



Aug 10 '06 #4

P: n/a
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,
--
Joerg Jooss
ne********@joergjooss.de
Aug 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.