Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageHi 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, bu t >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 > > >
Post Follow-up to this messageThe 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 .NET .NET >
Post Follow-up to this message
"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>
Post Follow-up to this messageHi 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, bu t >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 > > >
Post Follow-up to this message
"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>
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.