Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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.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





Report this thread to moderator Post Follow-up to this message
Old Post
Adam Stirk
08-21-04 09:01 PM


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.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
>
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
Scott Allen
08-21-04 09:01 PM


Re: Problems with WebRequest
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 
.NET 
.NET 
>




Report this thread to moderator Post Follow-up to this message
Old Post
Adam Stirk
08-21-04 09:01 PM


Re: Problems with WebRequest
"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>



Report this thread to moderator Post Follow-up to this message
Old Post
vMike
08-24-04 09:41 PM


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.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
>
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
Scott Allen
08-26-04 09:05 PM


Re: Problems with WebRequest
"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>



Report this thread to moderator Post Follow-up to this message
Old Post
vMike
08-31-04 08:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C# archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:45 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.