Problems with WebRequest

A

Adam Stirk

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
 
S

Scott Allen

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,
 
A

Adam Stirk

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 said:
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

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
 
V

vMike

Adam Stirk said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top