help making a http request from a C# library

  • Thread starter Thread starter mt404
  • Start date Start date
M

mt404

Hi,

I was wondering if someone might be able to provide some guidance on
how I could make an http request from a C# library.

Basically I have a library which accepts a couple of arguments that I
use to construct a dynamic http URL address. I wan't to then make a
call to this address, capture the data stream it returns, and return
that to the application which called the library. I know how to do
most everything but I'm not sure how to make an http request.

Could anyone shed any light on this? Or, perhaps even point me in the
direction of the class that aloows me to make http requests.

Thanks!
 
Could anyone shed any light on this? Or, perhaps even point me in the
direction of the class that aloows me to make http requests.


If you just want do download a file, try the DownloadFile method of the
WebClient class.
For more control, check out the HttpRequest class.

daniel
 
Sure,


HttpWebRequest req = (HttpWebRequest)WebRequest.Create("<your addr, port,
etc...>");


Stream s = req.GetResponse().GetResponseStream();

StreamReader sr = new StreamReader(s);

string str = sr.ReadToEnd();

Sorry if it's not exactly perfect but I'm doing it from memory. If you need
to 'POST' form fields/parameters, you can grab the 'GetRequestStream()' from
the HttpWebRequest object and write to it pretty easily.



Alex
 
If you just want do download a file, try the DownloadFile method of the
WebClient class.
For more control, check out the HttpRequest class.

daniel

Thanks Daniel, I'll take a look at this when I get back to the
project tomorrow morning. I'm sure there's plenty of information and
examples in the Visual Studio .NET docs.

Thanks for the shove in the right direction.
 
I was wondering if someone might be able to provide some guidance on
how I could make an http request from a C# library.

static void Main(string[] args)
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
string getDetails = "Argument=SomeTestArguments";
ASCIIEncoding sendEncoding = new ASCIIEncoding();
byte[] byte1 = sendEncoding.GetBytes(getDetails);

myHttpWebRequest.Method = "POST";
// Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
// Set the content length of the string being posted.
myHttpWebRequest.ContentLength = getDetails.Length;

Stream sendStream = myHttpWebRequest.GetRequestStream();
sendStream.Write(byte1,0,byte1.Length);
sendStream.Close();

// info
Console.WriteLine("The value of 'ContentLength' property after sending
the data is {0}", myHttpWebRequest.ContentLength);


// Dump the response
WebResponse myWebResponse = myHttpWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding readEncoding = System.Text.Encoding.GetEncoding("utf-8");

// Pipe the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader(ReceiveStream, readEncoding);
Console.WriteLine("\nResponse stream received");
Char[] read = new Char[256];

// Read 256 charcters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");

while (count > 0)
{
// Dump the 256 characters on a string and display the string
onto the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}

Console.WriteLine("");
// Release the resources of stream object.
readStream.Close();

// Release the resources of response object.
myWebResponse.Close();
}
}
 
Back
Top