Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using WebClient or WebRequest in web method.

11 views
Skip to first unread message

Jensen Bredhal

unread,
Nov 17, 2004, 8:51:26 AM11/17/04
to

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]

unread,
Nov 17, 2004, 12:12:48 PM11/17/04
to
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


"Jensen Bredhal" <first...@yahoo.com> wrote in message
news:OpOyY1Kz...@TK2MSFTNGP10.phx.gbl...

Jensen Bredal

unread,
Nov 18, 2004, 9:03:12 AM11/18/04
to
thousands thanks...


JB


"Dino Chiesa [Microsoft]" <din...@online.microsoft.com> wrote in message
news:eTBTAlMz...@TK2MSFTNGP12.phx.gbl...

Jensen Bredal

unread,
Nov 18, 2004, 9:11:40 AM11/18/04
to
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]

unread,
Nov 18, 2004, 10:31:43 AM11/18/04
to
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


"Jensen Bredal" <first...@yahoo.dk> wrote in message
news:%23bXJKiX...@TK2MSFTNGP14.phx.gbl...

Drew Marsh

unread,
Nov 18, 2004, 11:00:05 AM11/18/04
to
Jensen Bredal wrote:

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

unread,
Nov 19, 2004, 8:43:46 AM11/19/04
to
Great!

Many thanks...


0 new messages