HttpWebRequest headers problem

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Dunc (dunc_at_ntpcl.f9.co.uk)
Date: 09/17/04


Date: Fri, 17 Sep 2004 10:35:20 +0100

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();



Relevant Pages

  • Mysterious delay on first use of HttpWebRequest.GetRequestStream
    ... // Create an HTTP request to do the POST ... // we must close the stream to actually initiate sending the request ... These changes made no difference - there's still a long delay on the first ... HTTP access over the VPN connection to the same machine doesn't appear to ...
    (microsoft.public.dotnet.framework)
  • Re: Event sink registration via WebDAV
    ... string myExchange = "http://exdevel2k3/exchange/";; ... HttpWebRequest HttpWRequest = WebRequest.Create; ... // Set the request timeout to 5 minutes ... Stream QueryStream = HttpWRequest.GetRequestStream; ...
    (microsoft.public.exchange2000.development)
  • Re: problem WebRequest and WebResponse
    ... the Stream and the WebResponse instances. ... HttpWebRequest request = ... WebResponse response = null; ...
    (microsoft.public.dotnet.languages.csharp)
  • Event sink registration via WebDAV
    ... The exception: "The remote server returned an error Method not allowed" at line 68. ... string myExchange = "http://exdevel2k3/exchange/";; ... // Set the request timeout to 5 minutes ... Stream QueryStream = HttpWRequest.GetRequestStream; ...
    (microsoft.public.exchange2000.development)
  • Custom object passed to a remote object method - SecurityException
    ... Because of security restrictions, ... Unhandled Exception: System.Runtime.Serialization.SerializationException: ... ---> System.Security.SecurityException: Request failed. ... (String objectUri, Stream inputStream, Boolean bStrictBinding, ...
    (microsoft.public.dotnet.framework.remoting)