Tag: webclient

如何防止WebClient类自动跟踪标题中的位置?

是否可以在WebClient类中使用? 例如: MyWebClient.AllowAutoRedirect = false; (of HttpWebRequest)

如何设置WebClient Content-Type标头?

要连接到第三方服务,我需要制作一个Https Post。 其中一个必备条件是发送自定义内容类型。 我正在使用WebClient,但我找不到如何设置它。 我已经尝试创建一个新类并重写CreateRequest方法,但这会导致请求崩溃。 有没有办法做到这一点,而无需重写CopyHeadersTo方法? 提前致谢 编辑 CopyHeaderTo是我见过的使用.NET Reflector的方法。 它从GetWebRequest中取消,并从私有属性设置所有请求标头,包括Content-Type。

获取Content-Disposition参数

如何使用WebClient获取从WebAPI控制器返回的Content-Disposition参数? WebApi控制器 [Route(“api/mycontroller/GetFile/{fileId}”)] public HttpResponseMessage GetFile(int fileId) { try { var file = GetSomeFile(fileId) HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(new MemoryStream(file)); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue(“attachment”); response.Content.Headers.ContentDisposition.FileName = file.FileOriginalName; /********* Parameter *************/ response.Content.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue(“MyParameter”, “MyValue”)); return response; } catch(Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex); } } 客户 void DownloadFile() { WebClient wc = […]

如何调用WebBrowser导航来浏览一些url?

要收集网页上的信息,我可以使用WebBrowser.Navigated事件。 首先,导航到url: WebBrowser wbCourseOverview = new WebBrowser(); wbCourseOverview.ScriptErrorsSuppressed = true; wbCourseOverview.Navigate(url); wbCourseOverview.Navigated += wbCourseOverview_Navigated; 然后在调用Navigated时处理网页: void wbCourseOverview_Navigated(object sender, WebBrowserNavigatedEventArgs e) { //Find the control and invoke “Click” event… } 当我尝试通过字符串数组的url时,困难的部分出现了。 foreach (var u in courseUrls) { WebBrowser wbCourseOverview = new WebBrowser(); wbCourseOverview.ScriptErrorsSuppressed = true; wbCourseOverview.Navigate(u); wbCourseOverview.Navigated += wbCourseOverview_Navigated; } 在这里,因为页面加载需要时间, wbCourseOverview_Navigated永远不会达到wbCourseOverview_Navigated 。 我试图在C#5中使用async await 。 […]

正确处理两个WebException

我正在尝试正确处理两个不同的WebException 。 基本上他们在调用WebClient.DownloadFile(string address, string fileName) 到目前为止,AFAIK有两个我要处理的,都是WebException的: 无法解析远程名称(即没有网络连接访问服务器下载文件) (404)文件不正确(即服务器上不存在该文件) 可能会有更多,但这是我迄今为止最重要的。 那么我应该如何正确处理它,因为它们都是WebException ,但我想以不同的方式处理上面的每个案例。 这是我到目前为止: try { using (var client = new WebClient()) { client.DownloadFile(“…”); } } catch(InvalidOperationException ioEx) { if (ioEx is WebException) { if (ioEx.Message.Contains(“404”) { //handle 404 } if (ioEx.Message.Contains(“remote name could not”) { //handle file doesn’t exist } } } 正如您所看到的,我正在检查消息以查看它是什么类型的WebException。 我会假设有更好或更精确的方法来做到这一点? 谢谢

如何更改webClient.UploadData()的时间限制?

我正在使用WebClient.UploadData()在Java服务器上发布post。 我该如何延长时限? (每次我尝试进行一些调试时都会超时)

WebClient不会自动重定向

使用Firebug登录登录过程时,我发现它是这样的 POST //The normal post request GET //Automatically made after the login GET //Automatically made after the login GET //Automatically made after the login 使用下面的代码发出post请求时,它没有发出浏览器正在执行的自动GET请求。 我的WebClient处理程序 using System; using System.Net; namespace Test { class HttpHandler : WebClient { private CookieContainer _mContainer = new CookieContainer(); protected override WebRequest GetWebRequest(Uri address) { var request = base.GetWebRequest(address); if […]

WebClient AsyncUpload进度百分比始终返回50%

我使用Webclient使用异步调用上传数据到服务器, WebClient webClient = new WebClient(); webClient.UploadDataAsync(uri , “PUT”, buffer, userToken); 我已将DatauploadProgress和DatauploadCompleted事件附加到适当的回调函数 // Upload Date Progress webClient.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback); // Upload Date Progress void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e) { // Magic goes here logger.writeToLog(“Percentage =” + e.ProgressPercentage); } e.ProgressPercentage总是返回50 ..无论上传的文件大小是多少(尝试10kb到60mb之间的不同大小)。 函数本身只被调用两次(真的很快),百分比显示50! ..特别是大文件不合逻辑…… e.BytesSent也没有帮助..总是以字节为单位显示文件大小:S(例如:如果文件大小为63,000,我会得到e.BytesSent = 63,000和e.ProgressPercentage= 50 有人能指出问题给我吗?

错误请求400 – 有效URL上的协议错误 – Webclient

我正在尝试使用webclient解析此页面( http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47 )并且没有运气。 var client = new WebClient(); var html = client.DownloadString(“http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47”);

使用WebClient或HttpClient下载文件?

我试图从URL下载文件,我必须在WebClient和HttpClient之间进行选择。 我在互联网上引用了这篇文章和其他几篇文章。 无处不在,由于其强大的异步支持和其他.Net 4.5权限,建议使用HttpClient。 但我仍然不完全相信并需要更多的投入。 我使用下面的代码从互联网上下载文件: Web客户端: WebClient client = new WebClient(); client.DownloadFile(downloadUrl, filePath); HttpClient的: using (HttpClient client = new HttpClient()) { using (HttpResponseMessage response = await client.GetAsync(url)) using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync()) { } } 从我的角度来看,我只能看到使用WebClient的一个缺点,那就是非异步调用,阻塞调用线程。 但是,如果我不担心阻塞线程或使用client.DownloadFileAsync()来利用异步支持,该怎么办? 另一方面,如果我使用HttpClient,是不是我将文件的每个字节加载到内存中然后将其写入本地文件? 如果文件太大,内存开销不会很贵吗? 如果我们使用WebClient可以避免这种情况,因为它将直接写入本地文件而不会消耗系统内存。 那么,如果性能是我的首要任务,我应该使用哪种方法进行下载? 如果我的上述假设是错误的,我想澄清一下,我也愿意接受替代方法。