Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / November 2004

Tip: Looking for answers? Try searching our database.

Using WebClient or WebRequest in web method.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jensen Bredhal - 17 Nov 2004 13:51 GMT
Hello,

I need to send command to a web server from a method.

I understand this should be possible using the WebClient or WebRequest
class.

how can this be done?

below an example of my HTTP based command to the server:

An API invocation is simply an HTTP request (GET or POST), like this:

http://www.mydomain.com/desknow/admin?pwd=password&action=im_sendalert&user=joe&
domain=mydomain.com&message=emergency%20evacuation&alertcode=1

Dino Chiesa [Microsoft] - 17 Nov 2004 17:12 GMT
Get:
   public static string GetURI(string URI) {
     System.Net.WebRequest myWebRequest =
System.Net.WebRequest.Create(URI);
     myWebRequest.Proxy = new System.Net.WebProxy(ProxyString, true);  //
true == don't use proxy for local addresses
     System.Net.WebResponse response = myWebRequest.GetResponse();
     System.IO.StreamReader reader = new System.IO.StreamReader
(response.GetResponseStream());
     return reader.ReadToEnd().Trim();
   }

Post is different
 public static string PostURI(string URI, string Parameters) {
   System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
   req.Proxy = new System.Net.WebProxy(ProxyString, true);  // true ==
don't use proxy for local addresses
   req.ContentType = "application/x-www-form-urlencoded";
   req.Method = "POST";
   byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
   req.ContentLength = bytes.Length;
   System.IO.Stream OutputStream = req.GetRequestStream ();
   OutputStream.Write (bytes, 0, bytes.Length);
   OutputStream.Close ();
   System.Net.WebResponse resp = req.GetResponse();
   if (resp== null) return null;
   System.IO.StreamReader MyStreamReader = new
System.IO.StreamReader(resp.GetResponseStream());
   return MyStreamReader.ReadToEnd().Trim();
}

If you need authentication, you need to add that yourself.

-D

> Hello,
>
[quoted text clipped - 10 lines]
>
> http://www.mydomain.com/desknow/admin?pwd=password&action=im_sendalert&user=joe&
domain=mydomain.com&message=emergency%20evacuation&alertcode=1
Jensen Bredal - 18 Nov 2004 14:03 GMT
thousands thanks...

JB

> Get:
>     public static string GetURI(string URI) {
[quoted text clipped - 43 lines]
> >
> > An API invocation is simply an HTTP request (GET or POST), like this:

http://www.mydomain.com/desknow/admin?pwd=password&action=im_sendalert&user=joe&
domain=mydomain.com&message=emergency%20evacuation&alertcode=1

Jensen Bredal - 18 Nov 2004 14:11 GMT
Now i'm a bit more confused then before. If this request needs several
parameters as it is the case for my situation ,
how can i use your code?
Could you please provide a simple example with the query i have provided in
my original post?

Many thanks
Dino Chiesa [Microsoft] - 18 Nov 2004 15:31 GMT
in the HTTP GET scenario, all params go on the URL itself.
you are responsible for formatting the URL in a way that makes sense for the
receiving server.
The link you posted looks fine.  Do a GetURI on it and see what you get
back.

For the PostURI, then you need to pass in a string of params, formatted like
so
  param1=foo&param2=bar&param3=gee

ref: http://www.jmarshall.com/easy/http/http_footnotes.html#urlencoding

the caller of the PostURI() method is responsible for formatting the params
in this format.
This is described here:
http://www.faqs.org/rfcs/rfc1866.html

if the data is more complex (eg, you want to upload files), you can do
multi-part data encoding, which is described here
http://www.faqs.org/rfcs/rfc2388.html

but you would need to change the code I provided in order to make this work.

-Dino

> Now i'm a bit more confused then before. If this request needs several
> parameters as it is the case for my situation ,
[quoted text clipped - 4 lines]
>
> Many thanks
Drew Marsh - 18 Nov 2004 16:00 GMT
> Now i'm a bit more confused then before. If this request needs several
> parameters as it is the case for my situation ,
> how can i use your code?
> Could you please provide a simple example with the query i have
> provided in
> my original post?

Well, you said yourself it's just an HTTP GET or POST, right? There's not gonna be any magic for you from WebClient/Request, you just need to build the querystring/form data yourself. Here's how you'd POST:

<codeSnippet language="C#">
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.mydomain.com/desknow/admin");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

using(StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
 writer.Write("pwd=password&action=im_sendalert&user=joe&domain=mydomain.com&message=emergency%20evacuation&alertcode=1");
}

WebResponse response = request.GetResponse();

// TODO: make sure it worked
</codeSnippet>

HTH,
Drew
Jensen Bredal - 19 Nov 2004 13:43 GMT
Great!

Many thanks...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.