469,602 Members | 1,962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

HttpWebRequest headers problem

Hi,

I'm using a c# app to download zipped XML data from a 3rd party. All is
good, but the XML is generated on the remote machine at the time of request
(hence the huge timeout), and when it sends me the file, it has an error
message appended to the beginning:

<br />
<b>Warning</b>: HTTP_USER_AGENT variable is not set, cannot determine user
agent name in <b>/home/sites/site2/web/check_auth.inc.php</b> on line
<b>19</b><br />

I can successfully get the file if I do it manually through IE, so it's not
a problem on the client end. I've tried adding the HTTP_USER_AGENT header
to the request object, but no luck getting rid of it. How do I add the
correct request headers here? I've put the code (which is pretty simple)
below.

Thanks in advance,

Dunc

---/ snip /---

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strRemoteURI);

// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 64;
request.Timeout = 600000;

// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
request.Headers.Add("HTTP_USER_AGENT", "IE");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();

// Pipes the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader (receiveStream,
System.Text.Encoding.Unicode);
StreamWriter writeStream = new StreamWriter("_" + strFileName, false,
System.Text.Encoding.Unicode);

writeStream.Write(readStream.ReadToEnd());
writeStream.Flush();

response.Close();
readStream.Close();
writeStream.Close();
Nov 16 '05 #1
2 16875
Dunc wrote:
Hi,

I'm using a c# app to download zipped XML data from a 3rd party. All
is good, but the XML is generated on the remote machine at the time
of request (hence the huge timeout), and when it sends me the file,
it has an error message appended to the beginning:

<br />
<b>Warning</b>: HTTP_USER_AGENT variable is not set, cannot
determine user agent name in
<b>/home/sites/site2/web/check_auth.inc.php</b> on line <b>19</b><br
/>

I can successfully get the file if I do it manually through IE, so
it's not a problem on the client end. I've tried adding the
HTTP_USER_AGENT header to the request object, but no luck getting rid
of it. How do I add the correct request headers here? I've put the
code (which is pretty simple) below. [...]

HTTP_USER_AGENT is a probably a CGI variable, but not a HTTP header name.
Use
HttpWebRequest.UserAgent to set the user agent.

And "IE" is not a proper user agent string. Try
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"
-- that's IE 6 SP2 on Win XP SP2.

---/ snip /---

HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(strRemoteURI);

// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 64;
request.Timeout = 600000;

// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
request.Headers.Add("HTTP_USER_AGENT", "IE");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();

// Pipes the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader (receiveStream,
System.Text.Encoding.Unicode);
StreamWriter writeStream = new StreamWriter("_" + strFileName, false,
System.Text.Encoding.Unicode);


Ouch! Don't use Readers and Writers to handle binary content.

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #2
Dunc <du**@ntpcl.f9.co.uk> wrote:
I'm using a c# app to download zipped XML data from a 3rd party. All is
good, but the XML is generated on the remote machine at the time of request
(hence the huge timeout), and when it sends me the file, it has an error
message appended to the beginning:


HTTP_USER_AGENT isn't the header name, it's User-Agent.

Use the UserAgent property of HttpWebRequest to set it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by TJO | last post: by
1 post views Thread by Craig | last post: by
16 posts views Thread by thomas peter | last post: by
6 posts views Thread by Mike Koerner | last post: by
reply views Thread by Alex Papadimoulis | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.