473,549 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cre ate(strRemoteUR I);

// Set some reasonable limits on resources used by this request
request.Maximum AutomaticRedire ctions = 4;
request.Maximum ResponseHeaders Length = 64;
request.Timeout = 600000;

// Set credentials to use for this request.
request.Credent ials = CredentialCache .DefaultCredent ials;
request.Headers .Add("HTTP_USER _AGENT", "IE");
HttpWebResponse response = (HttpWebRespons e)request.GetRe sponse();

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

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

writeStream.Wri te(readStream.R eadToEnd());
writeStream.Flu sh();

response.Close( );
readStream.Clos e();
writeStream.Clo se();
Nov 16 '05 #1
2 17116
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.Cre ate(strRemoteUR I);

// Set some reasonable limits on resources used by this request
request.Maximum AutomaticRedire ctions = 4;
request.Maximum ResponseHeaders Length = 64;
request.Timeout = 600000;

// Set credentials to use for this request.
request.Credent ials = CredentialCache .DefaultCredent ials;
request.Headers .Add("HTTP_USER _AGENT", "IE");
HttpWebResponse response = (HttpWebRespons e)request.GetRe sponse();

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

// Pipes the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader (receiveStream,
System.Text.Enc oding.Unicode);
StreamWriter writeStream = new StreamWriter("_ " + strFileName, false,
System.Text.Enc oding.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3393
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not establish secure channel for SSL/TLS" private void mainHttpCalls(string postData) { HttpWebRequest objRequest1 ; HttpWebRequest objRequest2 ;
1
14241
by: Craig | last post by:
I wrote a generic page to do HttpWebRequest operations based on 3 querystring parameters for the uri, username, and password for basic authentication pages. The page is designed to help with network management... You would use the page like this: http://localhost/webform1.aspx?uri=http://pagetomonitor.com&user=someuser&pass=somepass ...
16
12615
by: thomas peter | last post by:
I am building a precache engine... one that request over 100 pages on an remote server to cache them remotely... can i use the HttpWebRequest and WebResponse classes for this? or must i use the MSHTML objects to really load the HTML and request all of the images on site? string lcUrl = http://www.cnn.com; // *** Establish the request
6
4159
by: Mike Koerner | last post by:
Hi, I am having problems setting the HttpWebRequest Date header. I understand that it is a restricted header and I do receive the "This header must be modified with the appropriate property." Is there a way to make sure that the date header is sent over or a way to work around this exception? Here's a sample of the code:
0
1501
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the ASP content, and then just writes it to the page. In development and testing, there were no problems. Once deployed to production, after the...
0
2029
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the ASP content, and then just writes it to the page. In development and testing, there were no problems. Once deployed to production, after the...
4
12692
by: Natalia | last post by:
Hello, I need to provide the ability to post file and some form elements via our website (asp.net) to the third party website (asp page). On http://aspalliance.com/236#Page4 - I found great advices but still having troubles... it might some obvious error that I am making but I just dont see it. ==================FIRST -...
0
12760
by: barrybevel | last post by:
Hi, I'm trying to login to the www.vodafone.ie website using HttpWebRequest. It works fine with IE/Firefox and the .NET Web Control too, just not with my code. I think it's a redirect 302 problem. I'm using this code in a ASP.NET 2.0 application just in case that matters, maybe someone knows a better way to do this?
3
1499
by: stoogots2 | last post by:
Hi, I was using the WebClient class (to do some screen scraping) and I was forced to change to the HttpWebRequest/Response because I needed control over the http headers. I am having two problems that have got me stumped. I am using Visual Studio 2003, Framework 1.1. 1) Even though I set HttpWebRequest.KeepAlive=true, I am not getting a...
0
7554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7992
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7840
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1973
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1087
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.