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:
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...
JB
"Dino Chiesa [Microsoft]" <din...@online.microsoft.com> wrote in message
news:eTBTAlMz...@TK2MSFTNGP12.phx.gbl...
Many thanks
For the PostURI, then you need to pass in a string of params, formatted like
so
param1=foo¶m2=bar¶m3=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...
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
Many thanks...