473,976 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7378
> 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*******@hotm ail.com> schrieb im Newsbeitrag
news:67******** *************** ***@posting.goo gle.com... 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.Cre ate("<your addr, port,
etc...>");
Stream s = req.GetResponse ().GetResponseS tream();

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 'GetRequestStre am()' from
the HttpWebRequest object and write to it pretty easily.

Alex

"mt404" <mt*******@hotm ail.com> wrote in message
news:67******** *************** ***@posting.goo gle.com...
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.Cre ate("<your addr, port,
etc...>");
Stream s = req.GetResponse ().GetResponseS tream();

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 'GetRequestStre am()' from
the HttpWebRequest object and write to it pretty easily.

Alex

"mt404" <mt*******@hotm ail.com> wrote in message
news:67******** *************** ***@posting.goo gle.com...
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 myHttpWebReques t = (HttpWebRequest )WebRequest.Cre ate(url);
string getDetails = "Argument=SomeT estArguments";
ASCIIEncoding sendEncoding = new ASCIIEncoding() ;
byte[] byte1 = sendEncoding.Ge tBytes(getDetai ls);

myHttpWebReques t.Method = "POST";
// Set the content type of the data being posted.
myHttpWebReques t.ContentType = "applicatio n/x-www-form-urlencoded";
// Set the content length of the string being posted.
myHttpWebReques t.ContentLength = getDetails.Leng th;

Stream sendStream = myHttpWebReques t.GetRequestStr eam();
sendStream.Writ e(byte1,0,byte1 .Length);
sendStream.Clos e();

// info
Console.WriteLi ne("The value of 'ContentLength' property after sending
the data is {0}", myHttpWebReques t.ContentLength );
// Dump the response
WebResponse myWebResponse = myHttpWebReques t.GetResponse() ;
Stream ReceiveStream = myWebResponse.G etResponseStrea m();
Encoding readEncoding = System.Text.Enc oding.GetEncodi ng("utf-8");

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

// Read 256 charcters at a time.
int count = readStream.Read ( read, 0, 256 );
Console.WriteLi ne("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(s tr);
count = readStream.Read (read, 0, 256);
}

Console.WriteLi ne("");
// Release the resources of stream object.
readStream.Clos e();

// Release the resources of response object.
myWebResponse.C lose();
}
}
Nov 15 '05 #7

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

Similar topics

8
5498
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
1
296
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 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...
6
5021
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 for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
14
2570
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. Basically here's the process as it works now (synchronously): Person visits mywebsite.com and fills out a form mywebsite.com POSTs one request to providerwebsite.com mywebsite.com receives response
0
5594
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
1
3153
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 environment variables from the server. When I am typing the url of the html page in my browser, I am able to read the Environment variables from the server and getting the desired results. But, when I am making Http request through my window application,...
11
26689
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 and PEAR is in c:\wamp\php\pear I modified php.ini in the c:\wamp\php directory to reflect the actual path, but even stopping and restarting my server shows the c: \php5\pear path. I can't change it no matter what I do I also tried the...
1
2954
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 CGI application misbehaved by not returning a complete set of HTTP headers. The scripts are very long but here are the opening statements: The One that works .... #!C:\Perl\bin\perl.exe # openresolver.cgi # # OpenResolver - a CGI script for...
53
8470
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 language="javascript" type="text/javascript">
3
2224
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 that are SELECT elements (javascript-- one named arrivalcity and one named returncity). Basically they ask for a departure and return city and another menu appears with options based on that city. I can't seem to get the input from these fields in the...
0
10350
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10167
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11406
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10905
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8456
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6413
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3758
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.