Problems with WebRequest

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

    #1

    Problems with WebRequest

    Hi,

    I am trying to download a image that is generated by PHP using
    HttpWebRequest, I believe the server uses cookies to generate the image, but
    I keep getting the error image from the server.

    Can anyone help?

    The code I am using is :-

    Uri myUrl = new
    Uri("http://www.kingsofchao s.com/recruit.php?uni qid=84y75898");
    HttpWebRequest myReq = (HttpWebRequest ) WebRequest.Crea te(myUrl);
    myReq.CookieCon tainer = new CookieContainer ();

    myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
    CLR 1.0.3705)";
    myReq.KeepAlive = true;

    HttpWebResponse myResponse = (HttpWebRespons e) myReq.GetRespon se();
    Console.WriteLi ne("\nThe HttpHeaders are \n\n\tName\t\tV alue\n{0}",
    myResponse.Head ers);

    string mySession = myResponse.Head ers["Set-Cookie"];

    myResponse.Clos e();


    myReq = (HttpWebRequest )
    WebRequest.Crea te("http://www.kingsofchao s.com/imageclick.php" );
    myReq.CookieCon tainer = new CookieContainer ();
    myReq.Headers["Cookie"] = mySession;

    Console.WriteLi ne("\nThe HttpHeaders are \n\n\tName\t\tV alue\n{0}",
    myReq.Headers);

    myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
    CLR 1.0.3705)";
    myReq.KeepAlive = true;

    myResponse = (HttpWebRespons e) myReq.GetRespon se();


    pictureBox1.Ima ge = Image.FromStrea m(myResponse.Ge tResponseStream ());
    pictureBox1.Siz e = pictureBox1.Ima ge.Size;

    myResponse.Clos e();


    Thanks in advance

    Adam




  • Scott Allen

    #2
    Re: Problems with WebRequest

    Hi Adam:

    You should not have to set the Cookie header yourself. Using:

    CookieContainer cookies = new CookieContainer ();

    You can than assign the cookies instance to both webrequests and the
    cookies should be sent appropriately. For example, use the following
    line on both instances of myReq:

    myReg.CookieCon tainer = cookies;

    HTH,

    --
    Scott


    On Sat, 21 Aug 2004 14:05:21 +0100, "Adam Stirk" <.> wrote:
    [color=blue]
    >Hi,
    >
    >I am trying to download a image that is generated by PHP using
    >HttpWebRequest , I believe the server uses cookies to generate the image, but
    >I keep getting the error image from the server.
    >
    >Can anyone help?
    >
    >The code I am using is :-
    >
    >Uri myUrl = new
    >Uri("http://www.kingsofchao s.com/recruit.php?uni qid=84y75898");
    >HttpWebReque st myReq = (HttpWebRequest ) WebRequest.Crea te(myUrl);
    >myReq.CookieCo ntainer = new CookieContainer ();
    >
    >myReq.UserAgen t = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
    >CLR 1.0.3705)";
    >myReq.KeepAliv e = true;
    >
    >HttpWebRespons e myResponse = (HttpWebRespons e) myReq.GetRespon se();
    >Console.WriteL ine("\nThe HttpHeaders are \n\n\tName\t\tV alue\n{0}",
    >myResponse.Hea ders);
    >
    >string mySession = myResponse.Head ers["Set-Cookie"];
    >
    >myResponse.Clo se();
    >
    >
    >myReq = (HttpWebRequest )
    >WebRequest.Cre ate("http://www.kingsofchao s.com/imageclick.php" );
    >myReq.CookieCo ntainer = new CookieContainer ();
    >myReq.Header s["Cookie"] = mySession;
    >
    >Console.WriteL ine("\nThe HttpHeaders are \n\n\tName\t\tV alue\n{0}",
    >myReq.Headers) ;
    >
    >myReq.UserAgen t = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
    >CLR 1.0.3705)";
    >myReq.KeepAliv e = true;
    >
    >myResponse = (HttpWebRespons e) myReq.GetRespon se();
    >
    >
    >pictureBox1.Im age = Image.FromStrea m(myResponse.Ge tResponseStream ());
    >pictureBox1.Si ze = pictureBox1.Ima ge.Size;
    >
    >myResponse.Clo se();
    >
    >
    >Thanks in advance
    >
    >Adam
    >
    >
    >[/color]

    Comment

    • Adam Stirk

      #3
      Re: Problems with WebRequest

      The problem I have got is the website returns 3 cookies in the header, but
      when I call

      myReg.CookieCon tainer.GetCooki es();

      I only get 2, when there should be 3 as myResponse.Head ers["Set-Cookie"]
      returns 3 cookies.

      The example I supplied is the actual code I am trying to use, if you run it
      your self you will see this.

      HTH

      Adam


      "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
      news:p1qei0t1oi pepa1q7jdl9alu0 9pm4hk2ca@4ax.c om...[color=blue]
      > Hi Adam:
      >
      > You should not have to set the Cookie header yourself. Using:
      >
      > CookieContainer cookies = new CookieContainer ();
      >
      > You can than assign the cookies instance to both webrequests and the
      > cookies should be sent appropriately. For example, use the following
      > line on both instances of myReq:
      >
      > myReg.CookieCon tainer = cookies;
      >
      > HTH,
      >
      > --
      > Scott
      > http://www.OdeToCode.com
      >
      > On Sat, 21 Aug 2004 14:05:21 +0100, "Adam Stirk" <.> wrote:
      >[color=green]
      > >Hi,
      > >
      > >I am trying to download a image that is generated by PHP using
      > >HttpWebRequest , I believe the server uses cookies to generate the image,[/color][/color]
      but[color=blue][color=green]
      > >I keep getting the error image from the server.
      > >
      > >Can anyone help?
      > >
      > >The code I am using is :-
      > >
      > >Uri myUrl = new
      > >Uri("http://www.kingsofchao s.com/recruit.php?uni qid=84y75898");
      > >HttpWebReque st myReq = (HttpWebRequest ) WebRequest.Crea te(myUrl);
      > >myReq.CookieCo ntainer = new CookieContainer ();
      > >
      > >myReq.UserAgen t = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;[/color][/color]
      ..NET[color=blue][color=green]
      > >CLR 1.0.3705)";
      > >myReq.KeepAliv e = true;
      > >
      > >HttpWebRespons e myResponse = (HttpWebRespons e) myReq.GetRespon se();
      > >Console.WriteL ine("\nThe HttpHeaders are \n\n\tName\t\tV alue\n{0}",
      > >myResponse.Hea ders);
      > >
      > >string mySession = myResponse.Head ers["Set-Cookie"];
      > >
      > >myResponse.Clo se();
      > >
      > >
      > >myReq = (HttpWebRequest )
      > >WebRequest.Cre ate("http://www.kingsofchao s.com/imageclick.php" );
      > >myReq.CookieCo ntainer = new CookieContainer ();
      > >myReq.Header s["Cookie"] = mySession;
      > >
      > >Console.WriteL ine("\nThe HttpHeaders are \n\n\tName\t\tV alue\n{0}",
      > >myReq.Headers) ;
      > >
      > >myReq.UserAgen t = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;[/color][/color]
      ..NET[color=blue][color=green]
      > >CLR 1.0.3705)";
      > >myReq.KeepAliv e = true;
      > >
      > >myResponse = (HttpWebRespons e) myReq.GetRespon se();
      > >
      > >
      > >pictureBox1.Im age = Image.FromStrea m(myResponse.Ge tResponseStream ());
      > >pictureBox1.Si ze = pictureBox1.Ima ge.Size;
      > >
      > >myResponse.Clo se();
      > >
      > >
      > >Thanks in advance
      > >
      > >Adam
      > >
      > >
      > >[/color]
      >[/color]



      Comment

      • vMike

        #4
        Re: Problems with WebRequest


        "Adam Stirk" <.> wrote in message
        news:41274892$0 $7085$ed2619ec@ ptn-nntp-reader03.plus.n et...[color=blue]
        > Hi,
        >
        > I am trying to download a image that is generated by PHP using
        > HttpWebRequest, I believe the server uses cookies to generate the image,[/color]
        but[color=blue]
        > I keep getting the error image from the server.
        >
        > Can anyone help?
        >
        > The code I am using is :-
        >
        > Uri myUrl = new
        > Uri("http://www.kingsofchao s.com/recruit.php?uni qid=84y75898");
        > HttpWebRequest myReq = (HttpWebRequest ) WebRequest.Crea te(myUrl);
        > myReq.CookieCon tainer = new CookieContainer ();
        >
        > myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
        > CLR 1.0.3705)";
        > myReq.KeepAlive = true;
        >
        > HttpWebResponse myResponse = (HttpWebRespons e) myReq.GetRespon se();
        > Console.WriteLi ne("\nThe HttpHeaders are \n\n\tName\t\tV alue\n{0}",
        > myResponse.Head ers);
        >
        > string mySession = myResponse.Head ers["Set-Cookie"];
        >
        > myResponse.Clos e();
        >
        >
        > myReq = (HttpWebRequest )
        > WebRequest.Crea te("http://www.kingsofchao s.com/imageclick.php" );
        > myReq.CookieCon tainer = new CookieContainer ();
        > myReq.Headers["Cookie"] = mySession;
        >
        > Console.WriteLi ne("\nThe HttpHeaders are \n\n\tName\t\tV alue\n{0}",
        > myReq.Headers);
        >
        > myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
        > CLR 1.0.3705)";
        > myReq.KeepAlive = true;
        >
        > myResponse = (HttpWebRespons e) myReq.GetRespon se();
        >
        >
        > pictureBox1.Ima ge = Image.FromStrea m(myResponse.Ge tResponseStream ());
        > pictureBox1.Siz e = pictureBox1.Ima ge.Size;
        >
        > myResponse.Clos e();
        >
        >
        > Thanks in advance
        >
        > Adam
        >
        >
        >
        >[/color]
        Here is an vb example of getting the graphic and displaying it as an image
        on a website. Maybe it will help you.
        Mike

        <%@ Import Namespace="Syst em.Net" %>
        <%@ Import Namespace="Syst em.IO" %>
        <%@ Import Namespace="Syst em.Drawing" %>
        <%@ import Namespace="Syst em.Drawing.Imag ing" %>

        <html>
        <script language="vb" runat="server">
        Sub Page_Load(sende r as object, e as eventargs)
        Dim myURL as Uri = new Uri("http://www.kingsofchao s.com/imageclick.php" )
        Dim myReq as HttpWebRequest = WebRequest.Crea te(myUrl)

        myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
        CLR 1.0.3705)"
        myReq.KeepAlive = true
        Response.Conten tType = "image/jpeg"
        Dim myResponse as HttpWebResponse = myReq.GetRespon se()
        Dim mynewimage as system.drawing. image =
        system.drawing. image.fromstrea m(myResponse.Ge tResponseStream ())
        mynewimage.Save (Response.Outpu tStream, ImageFormat.Jpe g)

        end sub
        </script>
        <body>

        </body>
        </html>


        Comment

        Working...