Picturebox, Image.FromStream, WebRequest - Doesn't allways work

M

Mads Aggerholm

Hello Sirs

I hope you can give me a hint to this little problem.

The thing is, I have for a lang time enjoyed the comics on www.washintonpost.com,
and I allways start the day by going to their homepage.

One day, I thought: Why not just make a little program in C#, that
just shows me the .gif-picture of the strip?
And so I did. Or, more correctly, I tried to do.

Actually, it's quite simple. I want to show the strip of "Non
Sequitur" from the 30. of may 2008.
It can be seen directly in a browser, using this link:

http://wpcomics.washingtonpost.com/client/wpc/nq/2008/05/30/

Showing "Properties" thell me, the link directly to the .gif-file is
this:

http://wpcomics.washingtonpost.com/feature/08/05/30/nq080530.gif

The strip can be viewed in a picturebox this way:

picturebox1.Image = Image.FromStream(WebRequest.Create("http://
wpcomics.washingtonpost.com/feature/08/05/30/
nq080530.gif").GetResponse().GetResponseStream());

If I want another strip from another date, I just change the date-part
of the url.

So far, so good.

However, I'd like to see "Hagar the Horrible" too. Browser link to the
same date is:

http://www.washingtonpost.com/wp-sr...ng.html?name=Hagar_The_Horrible&date=20080530

But here starts the problem: "Properties" on the strip is:

http://est.rbma.com/content/Hagar_The_Horrible?date=20080530

Directly in a browser, it works fine.
But using it in a picturebox does not!

What can I do to make this work??
Any constructive input is welcome!

(It _should_ be possible! A friend told me once, that anything other
programs can do, I can do too in C#.)

Sincerely / Best regards
Mads Aggerholm
 
P

parez

Hello Sirs

I hope you can give me a hint to this little problem.

The thing is, I have for a lang time enjoyed the comics onwww.washintonpost.com,
and I allways start the day by going to their homepage.

One day, I thought: Why not just make a little program in C#, that
just shows me the .gif-picture of the strip?
And so I did. Or, more correctly, I tried to do.

Actually, it's quite simple. I want to show the strip of "Non
Sequitur" from the 30. of may 2008.
It can be seen directly in a browser, using this link:

http://wpcomics.washingtonpost.com/client/wpc/nq/2008/05/30/

Showing "Properties" thell me, the link directly to the .gif-file is
this:

http://wpcomics.washingtonpost.com/feature/08/05/30/nq080530.gif

The strip can be viewed in a picturebox this way:

picturebox1.Image = Image.FromStream(WebRequest.Create("http://
wpcomics.washingtonpost.com/feature/08/05/30/
nq080530.gif").GetResponse().GetResponseStream());

If I want another strip from another date, I just change the date-part
of the url.

So far, so good.

However, I'd like to see "Hagar the Horrible" too. Browser link to the
same date is:

http://www.washingtonpost.com/wp-srv/artsandliving/comics/king.html?n...

But here starts the problem: "Properties" on the strip is:

http://est.rbma.com/content/Hagar_The_Horrible?date=20080530

Directly in a browser, it works fine.
But using it in a picturebox does not!

What can I do to make this work??
Any constructive input is welcome!

(It _should_ be possible! A friend told me once, that anything other
programs can do, I can do too in C#.)

Sincerely / Best regards
Mads Aggerholm

Try setting the User-agent in the web request header. if that does
not work then use fiddler and see what your browser sends to the site
and you send the same info from you web request.
 
M

Mads Aggerholm

Hi Pete, thanks for the quick answer.

[...]
But here starts the problem: "Properties" on the strip is:

Directly in a browser, it works fine.
But using it in a picturebox does not!

Define "does not".  What, exactly, doesn't work?
It actually comes up with a little bitmap image in my picturebox
(that's also standard like if I try a date that doesn't exist) saying:
"Content Currently Unavailable".

I am afraid I can not explain it any better.
You didn't bother to describe any specifics about what's not working, so  

Sorry about that.
Actually I have made it very simple:
I've made a C#-form, with a picturebox in it.
The code is just this:

picturebox1.Image = Image.FromStream(WebRequest.Create("http://
est.rbma.com/content/Hagar_The_Horrible?
date=20080530").GetResponse().GetResponseStream());

And I get the failure-bitmap.

When I change the code to this:

picturebox1.Image = Image.FromStream(WebRequest.Create("http://
wpcomics.washingtonpost.com/feature/08/05/30/
nq080530.gif").GetResponse().GetResponseStream());

the result is the comic strip in the picturebox - no problems.
it's impossible for me to say for sure that this is going on.  But if it 
is, you'll have to make sure that your program retrieves the data in  
exactly the same way that a web browser would (getting the original page,  
setting the "referrer" for the graphics, etc.).  The simplest way to do  
this would be to run a network monitor program (the Wireshark program  
might be useful in this respect) to look at what data is sent and received 
by your web browser when you look at the comic in your web browser.

I'll give that a try.
Thanks for your input, Pete.

Sincerely / Best regards
Mads Aggerholm
 
P

parez

Try setting the User-agent in the web request header. if that does
not work then use fiddler and see what your browser sends to the site
and you send the same info from you web request.

I tried going to http://est.rbma.com/content/Hagar_The_Horrible?date=20080530
and i got an image which said no referer.

so i created an html page with that image as the source and opened
it and it worked.

which means that the image server is only checking for a referrer. so
if you change your code

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://
est.rbma.com/content/Hagar_The_Horrible?date=20080530");


request .Referer="Somecrap.com";

it mite work....
 
P

parez

I find it interesting that _any_ referrer is sufficient. Usually web
sites check the referrer to make sure it's actually their own web page
(prevents hijacking/embedding content in a non-hosted page).

Anyway, to the OP: I would guess that the suggestion from "parez" will in
fact solve your problem.

Pete

Well.. actually i tried that again. .i got no referer image again. I
must have got a cached imaged when i ran it locally from desktop.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://
est.rbma.com/content/Hagar_The_Horrible?date=20080530");

request .Referer="Somecrap.com";

so instead of somecrap.com OP (i dont know what that means) will have
to use washtingtonpost.com
 
M

Mads Aggerholm

Hi Parez,

Thanks for the suggestion:
HttpWebRequest    request = (HttpWebRequest)WebRequest.Create("http://
est.rbma.com/content/Hagar_The_Horrible?date=20080530");

            request .Referer="Somecrap.com";

so instead of somecrap.com OP (i dont know what that means) will have
to use  washtingtonpost.com- Skjul tekst i anførselstegn -
But how do I get this into the picturebox??

I tried this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://
est.rbma.com/content/Hagar_The_Horrible?date=20080530");
request .Referer="washingtonpost.com";
picturebox1.Image =
Image.FromStream(request).GetResponse().GetResponseStream());

Wild guess, I thought it would work that way, but it didn't.

Sincerely / Best regards
Mads Aggerholm
 
M

Mads Aggerholm

Hi Pete,
Personally, it's my opinion that when one starts impersonating a web page, 
they are getting into a gray area with respect to content/copyright  
ethics.  The web server owner is clearly specifically interested in making  
sure that when you look at the comic, you do so in a particular context.  
When you impersonate that context, you break the implied contract with the 
content producer.

 From the content producer's point of view, that contract may involve  
ensuring that advertising or other contextual things that they deem  
important or financially necessary are included.  If you as the viewer  
aren't willing to abide by that contract, then you're basically saying you 
don't approve of the "sale", and thus shouldn't be "buying" (that is, just 
don't read the comic).

As far as I know, this is not technically illegal per se.  But it  
definitely seems a little "wrong" to me.  From an implementation point of  
view, this could be avoided by just using the WebBrowser control and  
displaying the entire original page rather than just the comic.  That  
would also automatically fix the "referrer" issue.  :)
I understand your thoughts on this issue, and I - partly - agree.
To my excuse, I can mention that I read those comics on the Post and
nothing else. The commercials, which is what the post wants me to
read, does not apply to me, since I am living in Denmark, and as I see
it, it's not much different from using an application that hides the
commercials on websites.

This project is something has it's offspring in the thougt, that it
would be interesting to see if it could be done.
That last part of the sentence is the greatest teacher in the world.
I have learned a million things that way.

Sincerely / Best regards
Mads Aggerholm
 
M

Mads Aggerholm

Ok, got it solved:

string myUri = "http://est.rbma.com/content/
Hagar_The_Horrible?date=20080526";
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create(myUri);
myHttpWebRequest.Referer = "http://www.washingtonpost.com/
wp-srv/artsandliving/comics";
try
{ HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
Stream streamResponse =
myHttpWebResponse.GetResponseStream();
Image im = Image.FromStream(streamResponse);
streamResponse.Close();
myHttpWebResponse.Close();
picturebox1.Image = im;
}
catch
{
MessageBox.Show("Failure!");
}

Thanks for the input, everybody!

Sincerely / Best regards
Mads Aggerholm
 

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