Re: HttpWebRequest headers problem

From: Joerg Jooss (joerg.jooss_at_gmx.net)
Date: 09/17/04


Date: Fri, 17 Sep 2004 11:40:26 +0200

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
joerg.jooss@gmx.net 


Relevant Pages

  • Re: WebControl/Cache-Control question
    ... the user agent, provided the user agent understands the header. ... The only thing I'd check is that you're setting this on the correct HTTP re- ... where the browser makes a second request that's separate to fetch the image. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: CGI module redirect defaults to 302 -- why?
    ... The response to the request can be found under a different URI ... POST-activated script to redirect the user agent to a selected ... 303 seems to be designed *exactly* for redirecting the browser after a CGI program has run. ...
    (comp.lang.perl.misc)
  • Re: Lots of noise about user agent strings
    ... gzipped JavaScript files to save bandwidth, ... principal difference one sees between altering over say ... Who is proposing not trussing the User Agent header? ... user agent originating the request. ...
    (comp.lang.javascript)
  • Re: using openurl to log into Yahoo services
    ... > I'm trying to write a script to log into Yahoo! ... > a resutling 400 - Bad Request page. ... It is possible that the "challenge" field is created by yahoo's server ... perhaps it contains a hash of the user agent that sent the request. ...
    (comp.lang.python)
  • HttpWebRequest headers problem
    ... I'm using a c# app to download zipped XML data from a 3rd party. ... to the request object, but no luck getting rid of it. ... // Get the stream associated with the response. ... Stream receiveStream = response.GetResponseStream; ...
    (microsoft.public.dotnet.languages.csharp)