For Programmers: Free Programming Magazines  


Home > Archive > C# > August 2004 > Problems with WebRequest









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problems with WebRequest
Adam Stirk

2004-08-21, 4:01 pm

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.kingsofchaos.com/recruit.php?uniqid=84y75898");
HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(myUrl);
myReq.CookieContainer = 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 = (HttpWebResponse) myReq.GetResponse();
Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",
myResponse.Headers);

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

myResponse.Close();


myReq = (HttpWebRequest)
WebRequest.Create("http://www.kingsofchaos.com/imageclick.php");
myReq.CookieContainer = new CookieContainer();
myReq.Headers["Cookie"] = mySession;

Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\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 = (HttpWebResponse) myReq.GetResponse();


pictureBox1.Image = Image.FromStream(myResponse.GetResponseStream());
pictureBox1.Size = pictureBox1.Image.Size;

myResponse.Close();


Thanks in advance

Adam




Scott Allen

2004-08-21, 4:01 pm

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.CookieContainer = cookies;

HTH,

--
Scott
http://www.OdeToCode.com

On Sat, 21 Aug 2004 14:05:21 +0100, "Adam Stirk" <.> wrote:

>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.kingsofchaos.com/recruit.php?uniqid=84y75898");
>HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(myUrl);
>myReq.CookieContainer = 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 = (HttpWebResponse) myReq.GetResponse();
>Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",
>myResponse.Headers);
>
>string mySession = myResponse.Headers["Set-Cookie"];
>
>myResponse.Close();
>
>
>myReq = (HttpWebRequest)
>WebRequest.Create("http://www.kingsofchaos.com/imageclick.php");
>myReq.CookieContainer = new CookieContainer();
>myReq.Headers["Cookie"] = mySession;
>
>Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\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 = (HttpWebResponse) myReq.GetResponse();
>
>
>pictureBox1.Image = Image.FromStream(myResponse.GetResponseStream());
>pictureBox1.Size = pictureBox1.Image.Size;
>
>myResponse.Close();
>
>
>Thanks in advance
>
>Adam
>
>
>


Adam Stirk

2004-08-21, 4:01 pm

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

myReg.CookieContainer.GetCookies();

I only get 2, when there should be 3 as myResponse.Headers["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:p1qei0t1oipepa1q7jdl9alu09pm4hk2ca@
4ax.com...
> 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.CookieContainer = cookies;
>
> HTH,
>
> --
> Scott
> http://www.OdeToCode.com
>
> On Sat, 21 Aug 2004 14:05:21 +0100, "Adam Stirk" <.> wrote:
>
but[color=darkred]
..NET[color=darkred]
..NET[color=darkred]
>




vMike

2004-08-24, 4:41 pm


"Adam Stirk" <.> wrote in message
news:41274892$0$7085$ed2619ec@ptn-nntp-reader03.plus.net...
> 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.kingsofchaos.com/recruit.php?uniqid=84y75898");
> HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(myUrl);
> myReq.CookieContainer = 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 = (HttpWebResponse) myReq.GetResponse();
> Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",
> myResponse.Headers);
>
> string mySession = myResponse.Headers["Set-Cookie"];
>
> myResponse.Close();
>
>
> myReq = (HttpWebRequest)
> WebRequest.Create("http://www.kingsofchaos.com/imageclick.php");
> myReq.CookieContainer = new CookieContainer();
> myReq.Headers["Cookie"] = mySession;
>
> Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\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 = (HttpWebResponse) myReq.GetResponse();
>
>
> pictureBox1.Image = Image.FromStream(myResponse.GetResponseStream());
> pictureBox1.Size = pictureBox1.Image.Size;
>
> myResponse.Close();
>
>
> Thanks in advance
>
> Adam
>
>
>
>

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="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>

<html>
<script language="vb" runat="server">
Sub Page_Load(sender as object, e as eventargs)
Dim myURL as Uri = new Uri("http://www.kingsofchaos.com/imageclick.php")
Dim myReq as HttpWebRequest = WebRequest.Create(myUrl)

myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
CLR 1.0.3705)"
myReq.KeepAlive = true
Response.ContentType = "image/jpeg"
Dim myResponse as HttpWebResponse = myReq.GetResponse()
Dim mynewimage as system.drawing.image =
system.drawing.image.fromstream(myResponse.GetResponseStream())
mynewimage.Save(Response.OutputStream, ImageFormat.Jpeg)

end sub
</script>
<body>

</body>
</html>


Scott Allen

2004-08-26, 4:05 pm

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.CookieContainer = cookies;

HTH,

--
Scott
http://www.OdeToCode.com

On Sat, 21 Aug 2004 14:05:21 +0100, "Adam Stirk" <.> wrote:

>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.kingsofchaos.com/recruit.php?uniqid=84y75898");
>HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(myUrl);
>myReq.CookieContainer = 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 = (HttpWebResponse) myReq.GetResponse();
>Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",
>myResponse.Headers);
>
>string mySession = myResponse.Headers["Set-Cookie"];
>
>myResponse.Close();
>
>
>myReq = (HttpWebRequest)
>WebRequest.Create("http://www.kingsofchaos.com/imageclick.php");
>myReq.CookieContainer = new CookieContainer();
>myReq.Headers["Cookie"] = mySession;
>
>Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\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 = (HttpWebResponse) myReq.GetResponse();
>
>
>pictureBox1.Image = Image.FromStream(myResponse.GetResponseStream());
>pictureBox1.Size = pictureBox1.Image.Size;
>
>myResponse.Close();
>
>
>Thanks in advance
>
>Adam
>
>
>


vMike

2004-08-31, 3:59 pm


"Adam Stirk" <.> wrote in message
news:41274892$0$7085$ed2619ec@ptn-nntp-reader03.plus.net...
> 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.kingsofchaos.com/recruit.php?uniqid=84y75898");
> HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(myUrl);
> myReq.CookieContainer = 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 = (HttpWebResponse) myReq.GetResponse();
> Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",
> myResponse.Headers);
>
> string mySession = myResponse.Headers["Set-Cookie"];
>
> myResponse.Close();
>
>
> myReq = (HttpWebRequest)
> WebRequest.Create("http://www.kingsofchaos.com/imageclick.php");
> myReq.CookieContainer = new CookieContainer();
> myReq.Headers["Cookie"] = mySession;
>
> Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\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 = (HttpWebResponse) myReq.GetResponse();
>
>
> pictureBox1.Image = Image.FromStream(myResponse.GetResponseStream());
> pictureBox1.Size = pictureBox1.Image.Size;
>
> myResponse.Close();
>
>
> Thanks in advance
>
> Adam
>
>
>
>

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="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>

<html>
<script language="vb" runat="server">
Sub Page_Load(sender as object, e as eventargs)
Dim myURL as Uri = new Uri("http://www.kingsofchaos.com/imageclick.php")
Dim myReq as HttpWebRequest = WebRequest.Create(myUrl)

myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
CLR 1.0.3705)"
myReq.KeepAlive = true
Response.ContentType = "image/jpeg"
Dim myResponse as HttpWebResponse = myReq.GetResponse()
Dim mynewimage as system.drawing.image =
system.drawing.image.fromstream(myResponse.GetResponseStream())
mynewimage.Save(Response.OutputStream, ImageFormat.Jpeg)

end sub
</script>
<body>

</body>
</html>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com