本文介绍了C#下载文件 - 错误400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个小工具,可以分析多个网站的网络结构,并提供下载某些媒体(如图像和视频)的选项。这并没有那么糟糕,但是我遇到了WebClient无法下载有效URL的特殊问题。我也尝试使用HttpWebRequest和HttpWebResponse设置进程,但同样的错误发生。没有InnerExceptions,例外是错误400(错误请求)。



警告:很抱歉,但发布的链接是色情的。我希望我有另一个URL,但这是我的错误日志中排序的唯一错误。我稍微修改了代码以简化它并仍然允许它重现错误。提供的URL可能会在一段时间后过期,但我已确定这是由 jizzhut.com生成的



WebClient代码:



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:

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代码:





HttpWebRequest code:

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();





我使用了UrlEncode和Uri.EscapeDataString / Uri .EscapeUriString,但是我使用它们错了,或者它们没有用。



I used the UrlEncode and the Uri.EscapeDataString / Uri.EscapeUriString, but either I''m using them wrong, or they''re not working.

推荐答案


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");







我测试了新代码,我认识到以下是网络请求失败的两个原因:




I tested new code and i recognize that here are 2 reasons for unsuccessful web request:

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();
}







理由:

1)%2F 表示 / ,但有时在网络请求中无法识别;你需要 [];使用以下提示: []

2)有时候无法找到请求的地址: im.d0634c1a.256020a.cdn2c.videos2.youjizz.com ;我不明白为什么......



对不起,我不能帮你;(

[/ EDIT]




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]


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.


这篇关于C#下载文件 - 错误400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 21:34
查看更多