473,378 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

help making a http request from a C# library

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!
Nov 15 '05 #1
6 7334
> 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

"mt404" <mt*******@hotmail.com> schrieb im Newsbeitrag
news:67**************************@posting.google.c om... 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!

Nov 15 '05 #2
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

"mt404" <mt*******@hotmail.com> wrote in message
news:67**************************@posting.google.c om...
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!

Nov 15 '05 #3
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

"mt404" <mt*******@hotmail.com> wrote in message
news:67**************************@posting.google.c om...
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!

Nov 15 '05 #4
> 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.
Nov 15 '05 #5
> 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.
Nov 15 '05 #6
> 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();
}
}
Nov 15 '05 #7

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

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
1
by: mt404 | last post by:
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...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
14
by: Mike C# | last post by:
Hi all, Is it possible to make 10 POST requests from ASP.NET asynchronously? I have been working on this problem for a few days now, and I seem to keep running up against IIS limitations. ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: Shashank | last post by:
Hi all, I am a new member of this community. I am making a http request to a html file placed on a Apache server. On this page there is an embeded perl statement which requires reading ...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
1
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
3
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.