Using WebClient or WebRequest in web method.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jensen Bredhal

    #1

    Using WebClient or WebRequest in web method.


    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:












  • Dino Chiesa [Microsoft]

    #2
    Re: Using WebClient or WebRequest in web method.

    Get:
    public static string GetURI(string URI) {
    System.Net.WebR equest myWebRequest =
    System.Net.WebR equest.Create(U RI);
    myWebRequest.Pr oxy = new System.Net.WebP roxy(ProxyStrin g, true); //
    true == don't use proxy for local addresses
    System.Net.WebR esponse response = myWebRequest.Ge tResponse();
    System.IO.Strea mReader reader = new System.IO.Strea mReader
    (response.GetRe sponseStream()) ;
    return reader.ReadToEn d().Trim();
    }

    Post is different
    public static string PostURI(string URI, string Parameters) {
    System.Net.WebR equest req = System.Net.WebR equest.Create(U RI);
    req.Proxy = new System.Net.WebP roxy(ProxyStrin g, true); // true ==
    don't use proxy for local addresses
    req.ContentType = "applicatio n/x-www-form-urlencoded";
    req.Method = "POST";
    byte [] bytes = System.Text.Enc oding.ASCII.Get Bytes(Parameter s);
    req.ContentLeng th = bytes.Length;
    System.IO.Strea m OutputStream = req.GetRequestS tream ();
    OutputStream.Wr ite (bytes, 0, bytes.Length);
    OutputStream.Cl ose ();
    System.Net.WebR esponse resp = req.GetResponse ();
    if (resp== null) return null;
    System.IO.Strea mReader MyStreamReader = new
    System.IO.Strea mReader(resp.Ge tResponseStream ());
    return MyStreamReader. ReadToEnd().Tri m();
    }

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

    -D


    "Jensen Bredhal" <first.last@yah oo.com> wrote in message
    news:OpOyY1KzEH A.3184@TK2MSFTN GP10.phx.gbl...[color=blue]
    >
    > 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/admi...on&alertcode=1
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Jensen Bredal

      #3
      Re: Using WebClient or WebRequest in web method.

      thousands thanks...


      JB


      "Dino Chiesa [Microsoft]" <dinoch@online. microsoft.com> wrote in message
      news:eTBTAlMzEH A.3120@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Get:
      > public static string GetURI(string URI) {
      > System.Net.WebR equest myWebRequest =
      > System.Net.WebR equest.Create(U RI);
      > myWebRequest.Pr oxy = new System.Net.WebP roxy(ProxyStrin g, true); //
      > true == don't use proxy for local addresses
      > System.Net.WebR esponse response = myWebRequest.Ge tResponse();
      > System.IO.Strea mReader reader = new System.IO.Strea mReader
      > (response.GetRe sponseStream()) ;
      > return reader.ReadToEn d().Trim();
      > }
      >
      > Post is different
      > public static string PostURI(string URI, string Parameters) {
      > System.Net.WebR equest req = System.Net.WebR equest.Create(U RI);
      > req.Proxy = new System.Net.WebP roxy(ProxyStrin g, true); // true ==
      > don't use proxy for local addresses
      > req.ContentType = "applicatio n/x-www-form-urlencoded";
      > req.Method = "POST";
      > byte [] bytes = System.Text.Enc oding.ASCII.Get Bytes(Parameter s);
      > req.ContentLeng th = bytes.Length;
      > System.IO.Strea m OutputStream = req.GetRequestS tream ();
      > OutputStream.Wr ite (bytes, 0, bytes.Length);
      > OutputStream.Cl ose ();
      > System.Net.WebR esponse resp = req.GetResponse ();
      > if (resp== null) return null;
      > System.IO.Strea mReader MyStreamReader = new
      > System.IO.Strea mReader(resp.Ge tResponseStream ());
      > return MyStreamReader. ReadToEnd().Tri m();
      > }
      >
      > If you need authentication, you need to add that yourself.
      >
      > -D
      >
      >
      > "Jensen Bredhal" <first.last@yah oo.com> wrote in message
      > news:OpOyY1KzEH A.3184@TK2MSFTN GP10.phx.gbl...[color=green]
      > >
      > > 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:
      > >
      > >[/color][/color]
      http://www.mydomain.com/desknow/admi...on&alertcode=1[color=blue][color=green]
      > >
      > >
      > >
      > >
      > >
      > >
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jensen Bredal

        #4
        Re: Using WebClient or WebRequest in web method.

        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





        Comment

        • Dino Chiesa [Microsoft]

          #5
          Re: Using WebClient or WebRequest in web method.

          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&para m2=bar&param3=g ee

          ref: http://www.jmarshall.com/easy/http/h...ml#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.last@yah oo.dk> wrote in message
          news:%23bXJKiXz EHA.2656@TK2MSF TNGP14.phx.gbl. ..[color=blue]
          > 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
          >
          >
          >
          >
          >[/color]


          Comment

          • Drew Marsh

            #6
            Re: Using WebClient or WebRequest in web method.

            Jensen Bredal wrote:
            [color=blue]
            > 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?[/color]

            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.Cre ate("http://www.mydomain.co m/desknow/admin");
            request.Method = "POST";
            request.Content Type = "applicatio n/x-www-form-urlencoded";

            using(StreamWri ter writer = new StreamWriter(re quest.GetReques tStream()))
            {
            writer.Write("p wd=password&act ion=im_sendaler t&user=joe&doma in=mydomain.com &message=emerge ncy%20evacuatio n&alertcode=1") ;
            }

            WebResponse response = request.GetResp onse();

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

            HTH,
            Drew

            Comment

            • Jensen Bredal

              #7
              Re: Using WebClient or WebRequest in web method.

              Great!

              Many thanks...


              Comment

              Working...