Click here to Skip to main content
15,887,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've made a little tool that is able to analyse the web structure of a number of sites and provide the option to download some of the media (such as images and video). This doesn't work so bad, but I'm having a particular problem with the WebClient failing to download a valid URL. I have also tried to set the process with HttpWebRequest and HttpWebResponse but the same error occurs. There are no InnerExceptions, and the exception is an Error 400 (Bad Request).

Warning: Sorry to say, but the posted link is pornographic. I wish I had another URL, but this is the only error of the sort in my error log. I have slightly modified the code to simplify it and still allow it to reproduce the error. The provided URL may expire after some time, but I have determined that this was generated from jizzhut.com

WebClient code:

C#
WebClient client = new WebClient();
client.DownloadFile("http://im.d0634c1a.256020a.cdn2c.videos2.youjizz.com/7%2F9%2F7931c1a64464599a38fbf1d39b64b62f1358483438-480-384-900-h264.flv?rs=300&ri=600&s=1363555197&e=1363641597&h=f1486952ad2905e7e3d2cd7b3137d1d1", "Vid.flv");


HttpWebRequest code:

C#
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://im.d0634c1a.256020a.cdn2c.videos2.youjizz.com/7%2F9%2F7931c1a64464599a38fbf1d39b64b62f1358483438-480-384-900-h264.flv?rs=300&ri=600&s=1363555197&e=1363641597&h=f1486952ad2905e7e3d2cd7b3137d1d1");
                            HttpWebResponse response = (HttpWebResponse)request.GetResponse();


I used the UrlEncode and the Uri.EscapeDataString / Uri.EscapeUriString, but either I'm using them wrong, or they're not working.
Posted
Comments
Jym Aklf 18-Mar-13 6:41am    
I have just used Fiddler 2 to monitor my requests and the responses, since I was suspecting that the browser was adding additional headers that my WebClient / HttpWebRequest were not generating. However with no headers, the request from the Fiddler still worked fine. I also tried calling the URL without the query strings and in this case the server did return a 400 error! Does this mean that the query strings are not getting sent? And how can I fix this?

In some strange cases, question marks etc. can be part of a filename.
I don't know if that happened here, too. Hence try to encode some or all of the special characters, e. g. ? -> %3F etc. (see http://www.blooberry.com/indexdot/html/topics/urlencoding.htm[^]).
It may also be the other way round: %2F stands for a slash /, and decoding that may be required.
 
Share this answer
 
Comments
Jym Aklf 18-Mar-13 7:59am    
Thanks for your response. I have been thinking that as well, since there is %2F in the URL, which stands for the slash. However I have tried replacing the %2F with the slash in the DownloadFile method, and it still fails.

I have also observed, that if I were to access the file path directly without the query strings (as such: http://im.d0634c1a.256020a.cdn2c.videos2.youjizz.com/7%2F9%2F7931c1a64464599a38fbf1d39b64b62f1358483438-480-384-900-h264.flv), in the browser it just displays an empty page. However if I were to try this in the WebClient, it would still throw the 400 exception, so I'm guessing there is something in the subdomain or in the actual file name on the server that is not being handled well in C#
Jym Aklf 18-Mar-13 8:40am    
Kindly check the comment I left on my own question. It shows more information relating to the query strings.
Probably your uri address is in wrong format (incorect Uri[^]). Try to run this:
C#
WebClient client = new WebClient();
Uri uriAddress = New Uri("http://im.d0634c1a.256020a.cdn2c.videos2.youjizz.com/7%2F9%2F7931c1a64464599a38fbf1d39b64b62f1358483438-480-384-900-h264.flv?rs=300&ri=600&s=1363555197&e=1363641597&h=f1486952ad2905e7e3d2cd7b3137d1d1")
client.DownloadFile(uriAddress, "Vid.flv");


[EDIT]
I tested new code and i recognize that here are 2 reasons for unsuccessful web request:
C#
try
{
    String sUri = "http://im.d0634c1a.256020a.cdn2c.videos2.youjizz.com/7%2F9%2F7931c1a64464599a38fbf1d39b64b62f1358483438-480-384-900-h264.flv?rs=300&ri=600&s=1363555197&e=1363641597&h=f1486952ad2905e7e3d2cd7b3137d1d1";
    Uri fullUri = new Uri(sUri);
    HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create(fullUri);
    Console.WriteLine("Media type :" + wrq.MediaType);
    HttpWebResponse wrs = (HttpWebResponse)wrq.GetResponse();
    Console.WriteLine("The encoding method used is: " + wrs.ContentEncoding);
    Console.WriteLine("The character set used is :" + wrs.CharacterSet);
    Console.WriteLine(wrs.ResponseUri);

}
catch (UriFormatException ex)
{
    Console.WriteLine(ex.Message);
}
catch (WebException ex)
{
    Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    Console.ReadLine();
}



Reasons:
1) %2F means "/", but sometimes it is not recognized in web request; you need to encode URL[^]; use tips from: http://www.jampmark.com/web-scripting/5-solutions-to-url-encoded-slashes-problem-in-apache.html[^]
2) sometimes there isn't possible to find requested addres: im.d0634c1a.256020a.cdn2c.videos2.youjizz.com; i have no idea why...

Sorry, but i can't help you ;(
[/EDIT]
 
Share this answer
 
v2
Comments
Jym Aklf 18-Mar-13 7:55am    
Hey. Thanks for your response. I have tried the code you submitted, but the DownloadFile method still fails :( . If I were to throw that URL in the browser, the file is found and downloaded. Do you have any more suggestions?
Maciej Los 18-Mar-13 8:14am    
Please, describe the error. What's fails?
Jym Aklf 18-Mar-13 8:37am    
I modified your code slightly, just to make it work:

WebClient client = new WebClient();
Uri uriAddress = new Uri("http://im.d0634c1a.256020a.cdn2c.videos2.youjizz.com/7%2F9%2F7931c1a64464599a38fbf1d39b64b62f1358483438-480-384-900-h264.flv?rs=300&ri=600&s=1363555197&e=1363641597&h=f1486952ad2905e7e3d2cd7b3137d1d1");
client.DownloadFile(uriAddress, "Vid.flv"); <-- This line throws a NetException Error 400 (Bad Request)
Jym Aklf 18-Mar-13 8:37am    
If you've got the time you can try it yourself, the problem should be replicable.
Maciej Los 18-Mar-13 16:37pm    
Interesting...
I'm not sure why this happens, but i promise to check this ;)
Hey. The URL you provided no longer appears to be valid, maybe it expired as you have mentioned, so I could not test it myself, but there are some situations in which WebClient / HttpWebRequest seem to fail, although I have no idea why unfortunately. So while I wish I could help you further in resolving it with the code you have provided, I cannot. Personally I would like to know why this happens, especially if the URL you provided was at one point functional.

What I can suggest though, since you seem to be looking for alternative approaches, is the URLDownloadToFile method assuming P/Invoke is fine by you. Take a look at: URLDownloadToFile[^]. It appears to be quite straight-forward and useful for your situation, since it requires no additional applications and can also download 'quietly'. In C# it can then be easily called as follows:

C#
using System.Runtime.InteropServices;

[DllImport("urlmon.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern Int32 URLDownloadToFile([MarshalAs(UnmanagedType.IUnknown)] object pCaller,
                                              [MarshalAs(UnmanagedType.LPWStr)] string szURL,
                                              [MarshalAs(UnmanagedType.LPWStr)] string szFileName,
                                              Int32 dwReserved, IntPtr lpfnCB);

URLDownloadToFile(null, "http://www.google.com.mt/images/srpr/logo4w.png", "C:\\Logo.png", 0, IntPtr.Zero);


Hope this has been helpful.
 
Share this answer
 
Comments
Jym Aklf 21-Mar-13 19:50pm    
P/Invoking is no problem for my scenario. I have tried it on a different URL, since that URL seems to be expired, and this worked out immediately! Thanks :)

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