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¶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
> 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