Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I am programing a module which download images from internet.
I am using code as below:

WebRequest wr = WebRequest.Create(url);
WebResponse ws = wr.GetResponse(); // => throw exception
Stream dataStream = ws.GetResponseStream();


But, there are some exception that I dont know how to resolve:

Url 1: http://dtgroup.com.vn/uploads/news/2011_08/ipd1311237144.gif
=> throw exception: The server committed a protocol violation. Section=ResponseHeader Detail=Header name is invalid

Url 2: http://cocdoc.fpt.edu.vn/sites/default/files/imagecache/318x191/anhtin/2_3.JPG
=> throw exception: The remote server returned an error: (403) Forbidden.

Infact, both url work on Firefox/Chrome, but my program cant download them.

Could you please show me some guides.
Thanks so much.
TuanNM
Posted
Comments
Sergey Alexandrovich Kryukov 9-Apr-12 22:33pm    
I see... reproduced the problem. Up-voted the question.
--SA

Some servers check for a valid "referer". Since you can access the images directly with Firefox, that is excluded.
Another idea is the "user agent". Try to set it in your application to Firefox (find out the correct wording!), perhaps that helps.
 
Share this answer
 
Comments
TuanNGUYEN 11-Apr-12 0:04am    
Thank so much,
The Url 2 work with "User Agent", but the Url 1 doesn't work.
I've tried some Referers, but the Url 1 doesn't work. Now I'm reading more about "Referer".
If you have experiences on setting Referer, could you please give me some guides.
Try putting this code in your app or web.config file :

XML
<system.net>
    <settings>
        <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
</system.net>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900