Grupy dyskusyjne Google nie obsługują już nowych postów ani subskrypcji z Usenetu. Treści historyczne nadal będą dostępne.

Using WebClient or WebRequest in web method.

11 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

Jensen Bredhal

nieprzeczytany,
17 lis 2004, 08:51:2617.11.2004
do

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]

nieprzeczytany,
17 lis 2004, 12:12:4817.11.2004
do
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

nieprzeczytany,
18 lis 2004, 09:03:1218.11.2004
do
thousands thanks...


JB


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

Jensen Bredal

nieprzeczytany,
18 lis 2004, 09:11:4018.11.2004
do
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]

nieprzeczytany,
18 lis 2004, 10:31:4318.11.2004
do
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

nieprzeczytany,
18 lis 2004, 11:00:0518.11.2004
do
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

nieprzeczytany,
19 lis 2004, 08:43:4619.11.2004
do
Great!

Many thanks...


Nowe wiadomości: 0