help making a http request from a C# library

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

    #1

    help making a http request from a C# library

    Hi,

    I was wondering if someone might be able to provide some guidance on
    how I could make an http request from a C# library.

    Basically I have a library which accepts a couple of arguments that I
    use to construct a dynamic http URL address. I wan't to then make a
    call to this address, capture the data stream it returns, and return
    that to the application which called the library. I know how to do
    most everything but I'm not sure how to make an http request.

    Could anyone shed any light on this? Or, perhaps even point me in the
    direction of the class that aloows me to make http requests.

    Thanks!
  • Daniel Weber

    #2
    Re: help making a http request from a C# library

    > Could anyone shed any light on this? Or, perhaps even point me in the[color=blue]
    > direction of the class that aloows me to make http requests.[/color]


    If you just want do download a file, try the DownloadFile method of the
    WebClient class.
    For more control, check out the HttpRequest class.

    daniel



    "mt404" <mtrinneer@hotm ail.com> schrieb im Newsbeitrag
    news:67be148b.0 309241243.1f986 dc6@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > I was wondering if someone might be able to provide some guidance on
    > how I could make an http request from a C# library.
    >
    > Basically I have a library which accepts a couple of arguments that I
    > use to construct a dynamic http URL address. I wan't to then make a
    > call to this address, capture the data stream it returns, and return
    > that to the application which called the library. I know how to do
    > most everything but I'm not sure how to make an http request.
    >
    > Could anyone shed any light on this? Or, perhaps even point me in the
    > direction of the class that aloows me to make http requests.
    >
    > Thanks![/color]


    Comment

    • Trebek

      #3
      Re: help making a http request from a C# library

      Sure,


      HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate("<your addr, port,
      etc...>");


      Stream s = req.GetResponse ().GetResponseS tream();

      StreamReader sr = new StreamReader(s) ;

      string str = sr.ReadToEnd();

      Sorry if it's not exactly perfect but I'm doing it from memory. If you need
      to 'POST' form fields/parameters, you can grab the 'GetRequestStre am()' from
      the HttpWebRequest object and write to it pretty easily.



      Alex





      "mt404" <mtrinneer@hotm ail.com> wrote in message
      news:67be148b.0 309241243.1f986 dc6@posting.goo gle.com...[color=blue]
      > Hi,
      >
      > I was wondering if someone might be able to provide some guidance on
      > how I could make an http request from a C# library.
      >
      > Basically I have a library which accepts a couple of arguments that I
      > use to construct a dynamic http URL address. I wan't to then make a
      > call to this address, capture the data stream it returns, and return
      > that to the application which called the library. I know how to do
      > most everything but I'm not sure how to make an http request.
      >
      > Could anyone shed any light on this? Or, perhaps even point me in the
      > direction of the class that aloows me to make http requests.
      >
      > Thanks![/color]


      Comment

      • Trebek

        #4
        Re: help making a http request from a C# library

        Sure,


        HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate("<your addr, port,
        etc...>");


        Stream s = req.GetResponse ().GetResponseS tream();

        StreamReader sr = new StreamReader(s) ;

        string str = sr.ReadToEnd();

        Sorry if it's not exactly perfect but I'm doing it from memory. If you need
        to 'POST' form fields/parameters, you can grab the 'GetRequestStre am()' from
        the HttpWebRequest object and write to it pretty easily.



        Alex





        "mt404" <mtrinneer@hotm ail.com> wrote in message
        news:67be148b.0 309241243.1f986 dc6@posting.goo gle.com...[color=blue]
        > Hi,
        >
        > I was wondering if someone might be able to provide some guidance on
        > how I could make an http request from a C# library.
        >
        > Basically I have a library which accepts a couple of arguments that I
        > use to construct a dynamic http URL address. I wan't to then make a
        > call to this address, capture the data stream it returns, and return
        > that to the application which called the library. I know how to do
        > most everything but I'm not sure how to make an http request.
        >
        > Could anyone shed any light on this? Or, perhaps even point me in the
        > direction of the class that aloows me to make http requests.
        >
        > Thanks![/color]


        Comment

        • mt404

          #5
          Re: help making a http request from a C# library

          > If you just want do download a file, try the DownloadFile method of the[color=blue]
          > WebClient class.
          > For more control, check out the HttpRequest class.
          >
          > daniel[/color]

          Thanks Daniel, I'll take a look at this when I get back to the
          project tomorrow morning. I'm sure there's plenty of information and
          examples in the Visual Studio .NET docs.

          Thanks for the shove in the right direction.

          Comment

          • mt404

            #6
            Re: help making a http request from a C# library

            > If you just want do download a file, try the DownloadFile method of the[color=blue]
            > WebClient class.
            > For more control, check out the HttpRequest class.
            >
            > daniel[/color]

            Thanks Daniel, I'll take a look at this when I get back to the
            project tomorrow morning. I'm sure there's plenty of information and
            examples in the Visual Studio .NET docs.

            Thanks for the shove in the right direction.

            Comment

            • Jeff Lindholm

              #7
              Re: help making a http request from a C# library

              > I was wondering if someone might be able to provide some guidance on[color=blue]
              > how I could make an http request from a C# library.
              >[/color]

              static void Main(string[] args)
              {
              HttpWebRequest myHttpWebReques t = (HttpWebRequest )WebRequest.Cre ate(url);
              string getDetails = "Argument=SomeT estArguments";
              ASCIIEncoding sendEncoding = new ASCIIEncoding() ;
              byte[] byte1 = sendEncoding.Ge tBytes(getDetai ls);

              myHttpWebReques t.Method = "POST";
              // Set the content type of the data being posted.
              myHttpWebReques t.ContentType = "applicatio n/x-www-form-urlencoded";
              // Set the content length of the string being posted.
              myHttpWebReques t.ContentLength = getDetails.Leng th;

              Stream sendStream = myHttpWebReques t.GetRequestStr eam();
              sendStream.Writ e(byte1,0,byte1 .Length);
              sendStream.Clos e();

              // info
              Console.WriteLi ne("The value of 'ContentLength' property after sending
              the data is {0}", myHttpWebReques t.ContentLength );


              // Dump the response
              WebResponse myWebResponse = myHttpWebReques t.GetResponse() ;
              Stream ReceiveStream = myWebResponse.G etResponseStrea m();
              Encoding readEncoding = System.Text.Enc oding.GetEncodi ng("utf-8");

              // Pipe the stream to a higher level stream reader with the required
              encoding format.
              StreamReader readStream = new StreamReader(Re ceiveStream, readEncoding);
              Console.WriteLi ne("\nResponse stream received");
              Char[] read = new Char[256];

              // Read 256 charcters at a time.
              int count = readStream.Read ( read, 0, 256 );
              Console.WriteLi ne("HTML...\r\n ");

              while (count > 0)
              {
              // Dump the 256 characters on a string and display the string
              onto the console.
              String str = new String(read, 0, count);
              Console.Write(s tr);
              count = readStream.Read (read, 0, 256);
              }

              Console.WriteLi ne("");
              // Release the resources of stream object.
              readStream.Clos e();

              // Release the resources of response object.
              myWebResponse.C lose();
              }
              }


              Comment

              Working...