473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

WebClient.UploadData

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
4 7729
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ramkrishna Kulkarni | last post by:
Hi, We are using UploadData method of WebClient class in C# to establish connection(s) between two machines(typical client/server). Connection is established if the machines are on same domain. If...
0
by: Al Cadalzo | last post by:
I'm using VS.Net v7.1.3052 I'm using WebClient to do a POST: Here's a code snippet: (C#) Byte responseArray = null; try
4
by: Paul J. Lay | last post by:
I am sending and receiving multipart messages using the WebClient UploadData method Method=Post. Everything seems to work well except when the URL contains parameters. For example:...
3
by: bss2004 | last post by:
Help! I'm posting a PDF Doc to a remote server using WebClient UploadData and the following code. The DOC posts fine and the server returns a positive response. If I access the remote file in...
0
by: Mark | last post by:
I have a very strange problem that is driving me nuts. My application is using the webclient object to send data to a webserver running on a unix box. We use 2 different environments, 1 for...
3
by: Philip Wagenaar | last post by:
Hello, I have a executeble that sends a request to a webserver: Return Encoding.UTF8.GetString(myWebClient.UploadData(uri, "POST", Encoding.UTF8.GetBytes(postString))) We are a having a...
1
by: Rippo | last post by:
Hi I need to post a form to an external URL, get a repsonse, then repost to an external URL and redirect at the same time. I can figure out step 1 and step 2 fine but I cant seem to figure out...
2
by: Tosco | last post by:
I read many examples with NetworkCredential and WebClient, but no one with a real http address. They all work in theory, but I wasn't able to use them in the real world. The following code should...
0
by: y2ktan | last post by:
Hi All, I try to use WebClient.UploadData to upload a photo with the size less than 1MB to my local IIS. I setup my IIS by adding a new virtual directory with the write access to the photo...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.