webclient

How to set HttpHeader on individual request using HttpClient

时光总嘲笑我的痴心妄想 提交于 2019-12-05 11:31:23
I have an HttpClient that is shared across multiple threads: public static class Connection { public static HttpClient Client { get; } static Connection() { Client = new HttpClient { BaseAddress = new Uri(Config.APIUri) }; Client.DefaultRequestHeaders.Add("Connection", "Keep-Alive"); Client.DefaultRequestHeaders.Add("Keep-Alive", "timeout=600"); Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } } It has some default headers I put onto each request. However, when I use it, I want to add on a header for just that request: var client = Connection

How to read an ASP.NET internal server error description with .NET?

删除回忆录丶 提交于 2019-12-05 10:10:12
Behold the code: using (var client = new WebClient()) { try { var bytesReceived = client.UploadData("http://localhost", bytesToPost); var response = client.Encoding.GetString(bytesReceived); } catch (Exception ex) { } } I am getting this HTTP 500 internal server error when the UploadData method is called. But I can't see the error description anywhere in the "ex" object while debugging. How do I rewrite this code so I can read the error description? Web servers often return an error page with more details (either HTML or plain text depending on the server). You can grab this by catching

Downloading .csv file from Google Trends

谁都会走 提交于 2019-12-05 09:43:49
I'm looking to download Google Trends data from URLs that I create via code. These URLs, when put into the browser (I use firefox) show a save prompt where I can choose to open or save the file when looking through the browser. However, I am hoping to the the same .csv file containing the Google Trends data via code, and I am stuck. Here is one of the links that my code creates: [Does prompt to download, however is is Google] https://www.google.com/trends/trendsReport?hl=en-US&q=debt&geo=US&date=now%207-d&cmpt=q&content=1&export=1 I have two issues with this link, It either downloads a .csv

WebClient Unicode - Which UTF8?

心已入冬 提交于 2019-12-05 08:20:34
问题 When I create a WebClient to consume some RESTful xml, I can specify the unicode encoding 2 ways: WebClient wc = new WebClient (); wc.Encoding = Encoding.UTF8; wc.Encoding = UTF8Encoding.UTF8; Which is correct/better ? 回答1: They're identical. UTF8Encoding inherits Encoding . Therefore, you can access all of the static members declared by Encoding through the UTF8Encoding qualifier. In fact, you can even write ASCIIEncoding.UTF8 , and it will still work. It will compile to identical IL, even

Downloading multiple files WebClient

落爺英雄遲暮 提交于 2019-12-05 08:11:19
问题 I'm trying to download multiple files but it's not working as I hoped. Can someone tell me what's wrong with this script, because I've tried a lot of things and really don't know what to do anymore. public static void DownloadFile(string url) { WebClient client = new WebClient(); var name = url.Substring(url.LastIndexOf('/')).Remove(0, 1); foreach (var item in urls) { client.DownloadFile(item, "C:\\" + name); } } private void btnGo_Click(object sender, EventArgs e) { urls.Add("url1"); urls

WebClient: Ignore HTTP 500

若如初见. 提交于 2019-12-05 06:56:14
I am writing a program which retrieves some data from a server, does some operations on it, and saves the output to a csv file. The problem I have is that the server (which I am not responsible for) ALWAYS returns an HTTP 500 internal server error. I have spoken to the team who look after it, and while they're aware of the bug they've said it's not impacting enough for them to resolve. Is there a way for me to ignore this response in my code and still get at the data? If you're using HttpWebRequest/Response, this should get you started: response = null; try { HttpWebRequest request =

How to use iTunes search for genres

折月煮酒 提交于 2019-12-05 06:10:02
问题 (I made a major edit as the C# code snippets were not helping but masked the issue - hopefully it is clearer now) I am trying to get listing of podcasts by genre from iTunes and using the API as described in https://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html the best I can. I have no difficulties in searcing for a name and parsing / using the response in JSON e.g.the search https://itunes.apple.com/search?term=Design&media=podcast&entity

detecting connection drops while WebClient is downloading a file asynchronously (in C#)

天涯浪子 提交于 2019-12-05 03:56:31
I am using WebClient's DownloadFileAsync (in C#) method to download files asynchronously. I have event handlers attached to DownloadProgressChanged and DownloadFileCompleted events. I hoped to get notified of any errors through AsyncCompletedEventArgs's Error property in the DownloadFileCompleted event handler. It works well if the connection is not present before the download begins. It craps out with proper error and I get the error in the property I mentioned above. But if the connection drops while the download is in progress nothing happens. The event handler is not called, it keeps

Progress bar and webclient

不羁的心 提交于 2019-12-05 01:57:54
问题 I have an event that takes about 10-30 seconds, namely downloading information from a page (with quite a lot of traffic), modifying it and then saving it somewhere onto the disk using WebClient. Because it takes such a long time, I want to add a progress bar or make an update label (which says something like updating..) to indicate the progress. Can someone guide me as to how I would do this? Is there any event in the WebClient I can use to handle this? 回答1: If you're writing a Windows Forms

How can I get WebClient (webservice client) to automatically use the default proxy server?

落花浮王杯 提交于 2019-12-05 00:59:55
I'm calling a webservice from a WinForms app. Everything works fine when a proxy server isn't in use, however when a proxy is being used, the app crashes as instead of the XML response it's expecting for the SOAP request, it gets an HTML error page saying "Authentication Required". It seems you can set the proxy manually like this: WebClient client = new WebClient(); WebProxy wp = new WebProxy("proxy server url here"); client.Proxy = wp; ...but to some extent, it seems to be seeing the proxy server anyway WITHOUT doing the above, as the error generated is actually coming from the proxy server.