HTTP request in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benoît Touron

    #1

    HTTP request in C#

    Hi !

    I am a newbie in C#. I am developping a web service in C#. I need to
    perform an HTTP request to another server, get the result, analyze it
    and return it to my client. How can I do this ?
    I did not find any sample in the MSDN, nor on the net.

    Thanks
    Ben

  • Adam D

    #2
    Re: HTTP request in C#

    Benoît Touron <b.touron@citb. bull.net> wrote in news:ujq0FPzsDH A.536
    @tk2msftngp13.p hx.gbl:
    [color=blue]
    > Hi !
    >
    > I am a newbie in C#. I am developping a web service in C#. I need to
    > perform an HTTP request to another server, get the result, analyze it
    > and return it to my client. How can I do this ?
    > I did not find any sample in the MSDN, nor on the net.
    >
    > Thanks
    > Ben
    >
    >[/color]

    you can add a web reference. The code will be generated for you. Or you can
    use the WSDL utility. Do a search on MSDN.Microsoft. com for it.

    --
    Adam D.
    adam@soaprp_no_ spam_please.com

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: HTTP request in C#

      Benoît Touron <b.touron@citb. bull.net> wrote:[color=blue]
      > I am a newbie in C#. I am developping a web service in C#. I need to
      > perform an HTTP request to another server, get the result, analyze it
      > and return it to my client. How can I do this ?
      > I did not find any sample in the MSDN, nor on the net.[/color]

      Have a look at the WebRequest class, in the System.Net namespace.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Trebek

        #4
        Re: HTTP request in C#

        Simple example follows :


        HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate(post_url);

        req.ContentType = "applicatio n/x-www-form-urlencoded";

        req.Method = "POST";

        //Now we need to read the data length for post

        req.ContentLeng th = sb.Length;


        //POST it

        Stream strWrite = req.GetRequestS tream();

        StreamWriter sw = new StreamWriter(st rWrite);

        sw.Write(sb.ToS tring());

        sw.Flush();

        sw.Close();

        //Read the response back in

        WebResponse wr = req.GetResponse ();

        HttpWebResponse httpRes = (HttpWebRespons e)wr;

        Stream s = httpRes.GetResp onseStream();

        StreamReader sr = new StreamReader(s, Encoding.ASCII) ;

        ret = sr.ReadToEnd();

        httpRes.Close() ;



        HTH,



        TREBEK


        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        news:MPG.1a2d36 d7af553487989b3 3@msnews.micros oft.com...
        Benoît Touron <b.touron@citb. bull.net> wrote:[color=blue]
        > I am a newbie in C#. I am developping a web service in C#. I need to
        > perform an HTTP request to another server, get the result, analyze it
        > and return it to my client. How can I do this ?
        > I did not find any sample in the MSDN, nor on the net.[/color]

        Have a look at the WebRequest class, in the System.Net namespace.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too


        Comment

        • Benoît Touron

          #5
          Re: HTTP request in C#

          Fine ! That's exactly what I need.
          Thanks a lot.
          Ben
          [color=blue]
          > Simple example follows :
          >
          >
          > HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate(post_url);
          >
          > req.ContentType = "applicatio n/x-www-form-urlencoded";
          >
          > req.Method = "POST";
          >
          > //Now we need to read the data length for post
          >
          > req.ContentLeng th = sb.Length;
          >
          >
          > //POST it
          >
          > Stream strWrite = req.GetRequestS tream();
          >
          > StreamWriter sw = new StreamWriter(st rWrite);
          >
          > sw.Write(sb.ToS tring());
          >
          > sw.Flush();
          >
          > sw.Close();
          >
          > //Read the response back in
          >
          > WebResponse wr = req.GetResponse ();
          >
          > HttpWebResponse httpRes = (HttpWebRespons e)wr;
          >
          > Stream s = httpRes.GetResp onseStream();
          >
          > StreamReader sr = new StreamReader(s, Encoding.ASCII) ;
          >
          > ret = sr.ReadToEnd();
          >
          > httpRes.Close() ;
          >
          >
          >
          > HTH,
          >
          >
          >
          > TREBEK
          >
          >
          > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          > news:MPG.1a2d36 d7af553487989b3 3@msnews.micros oft.com...
          > Benoît Touron <b.touron@citb. bull.net> wrote:
          >[color=green]
          >>I am a newbie in C#. I am developping a web service in C#. I need to
          >>perform an HTTP request to another server, get the result, analyze it
          >>and return it to my client. How can I do this ?
          >>I did not find any sample in the MSDN, nor on the net.[/color]
          >
          >
          > Have a look at the WebRequest class, in the System.Net namespace.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too
          >
          >[/color]

          Comment

          • Joerg Jooss

            #6
            Re: HTTP request in C#

            "Trebek" wrote:

            [...][color=blue]
            > //Now we need to read the data length for post
            >
            > req.ContentLeng th = sb.Length;[/color]

            Ouch -- kids, don't do that at home.

            First law of proper character string processing: String length in
            general does not equal byte length.

            Because...
            [color=blue]
            > //POST it
            >
            > Stream strWrite = req.GetRequestS tream();
            >
            > StreamWriter sw = new StreamWriter(st rWrite);[/color]

            ... posts UTF-8 encoded text with a byte order mark. Even if the posted
            text contained only US-ASCII characters, the actual content length would
            be sb.Length() + 3.
            [color=blue]
            > sw.Write(sb.ToS tring());
            >
            > sw.Flush();
            >
            > sw.Close();
            >
            > //Read the response back in
            >
            > WebResponse wr = req.GetResponse ();
            >
            > HttpWebResponse httpRes = (HttpWebRespons e)wr;
            >
            > Stream s = httpRes.GetResp onseStream();
            > StreamReader sr = new StreamReader(s, Encoding.ASCII) ;[/color]

            Second law of proper character string processing: Stay away from ASCII
            ;-)

            Seriously, when downloading web contents, at least assume content to be
            ISO-8851-9 encoded.

            Cheers,
            --
            Joerg Jooss
            joerg.jooss@gmx .net

            Comment

            Working...