HTTP request in C#

  • Thread starter Thread starter =?ISO-8859-1?Q?Beno=EEt_Touron?=
  • Start date Start date
?

=?ISO-8859-1?Q?Beno=EEt_Touron?=

Hi !

I am a newbie in C#. I am developping a web service in C#. I need to
perform an HTTP request to another server, get the result, analyze it
and return it to my client. How can I do this ?
I did not find any sample in the MSDN, nor on the net.

Thanks
Ben
 
Hi !

I am a newbie in C#. I am developping a web service in C#. I need to
perform an HTTP request to another server, get the result, analyze it
and return it to my client. How can I do this ?
I did not find any sample in the MSDN, nor on the net.

Thanks
Ben

you can add a web reference. The code will be generated for you. Or you can
use the WSDL utility. Do a search on MSDN.Microsoft.com for it.
 
Benoît Touron said:
I am a newbie in C#. I am developping a web service in C#. I need to
perform an HTTP request to another server, get the result, analyze it
and return it to my client. How can I do this ?
I did not find any sample in the MSDN, nor on the net.

Have a look at the WebRequest class, in the System.Net namespace.
 
Simple example follows :


HttpWebRequest req = (HttpWebRequest)WebRequest.Create(post_url);

req.ContentType = "application/x-www-form-urlencoded";

req.Method = "POST";

//Now we need to read the data length for post

req.ContentLength = sb.Length;


//POST it

Stream strWrite = req.GetRequestStream();

StreamWriter sw = new StreamWriter(strWrite);

sw.Write(sb.ToString());

sw.Flush();

sw.Close();

//Read the response back in

WebResponse wr = req.GetResponse();

HttpWebResponse httpRes = (HttpWebResponse)wr;

Stream s = httpRes.GetResponseStream();

StreamReader sr = new StreamReader(s,Encoding.ASCII);

ret = sr.ReadToEnd();

httpRes.Close();



HTH,



TREBEK


Benoît Touron said:
I am a newbie in C#. I am developping a web service in C#. I need to
perform an HTTP request to another server, get the result, analyze it
and return it to my client. How can I do this ?
I did not find any sample in the MSDN, nor on the net.

Have a look at the WebRequest class, in the System.Net namespace.
 
:

[...]
//Now we need to read the data length for post

req.ContentLength = sb.Length;

Ouch -- kids, don't do that at home.

First law of proper character string processing: String length in
general does not equal byte length.

Because...
//POST it

Stream strWrite = req.GetRequestStream();

StreamWriter sw = new StreamWriter(strWrite);

... posts UTF-8 encoded text with a byte order mark. Even if the posted
text contained only US-ASCII characters, the actual content length would
be sb.Length() + 3.
sw.Write(sb.ToString());

sw.Flush();

sw.Close();

//Read the response back in

WebResponse wr = req.GetResponse();

HttpWebResponse httpRes = (HttpWebResponse)wr;

Stream s = httpRes.GetResponseStream();
StreamReader sr = new StreamReader(s,Encoding.ASCII);

Second law of proper character string processing: Stay away from ASCII
;-)

Seriously, when downloading web contents, at least assume content to be
ISO-8851-9 encoded.

Cheers,
 
Back
Top