dotnet-httpclient

Xamarin Forms PCL - clean and easy way for a webrequest?

一个人想着一个人 提交于 2019-12-05 20:25:56
I am building an Android/iOS xamarin forms app with a portable class library. I am looking for the best way to do this example inside the PCL project: https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Create a request for the URL. WebRequest request = WebRequest.Create ( "http://www.contoso.com/default.html"); // If required by the server, set the credentials. request.Credentials = CredentialCache

C# async tasks waiting indefinitely

佐手、 提交于 2019-12-05 16:43:06
问题 I am trying to use the functionality provided by "async" & "await" to asynchronously download webpage content and I have into issues where the Tasks are waiting forever to complete. Could you please let me know what is wrong with the following code snippet? protected void Page_Load(object sender, EventArgs e) { var websites = new string[] {"http://www.cnn.com","http://www.foxnews.com"}; var tasks = websites.Select(GenerateSomeContent).ToList(); //I don't want to use 'await Tasks.WhenAll(tasks

Certificate error when configuring HttpClientFactory

不想你离开。 提交于 2019-12-05 14:52:38
I need to add certificate in HttpClientFactory . Old implementation with HttpClient look this: var cookieContainer = new CookieContainer(); var handler = new HttpClientHandler { CookieContainer = cookieContainer }; var basePath = Directory.GetCurrentDirectory(); var certificatePath = Path.Combine(basePath, certPath); var fileExists = File.Exists(certificatePath); if (!fileExists) throw new ArgumentException(certificatePath); var certificate = new X509Certificate2(certificatePath, certPwd); handler.ClientCertificates.Add(certificate); using (var client = new HttpClient(handler)) { client

C# HttpClient.SendAsync throw “An error occurred while sending the request” exception when testing some URLs

≯℡__Kan透↙ 提交于 2019-12-05 11:59:24
问题 I am developing an C# console application for testing whether a URL is valid or works. It works well for most of URLs and can get response with HTTP Status Code from target website. But when testing some other URLs, the application throw an "An error occurred while sending the request" exception when running HttpClient.SendAsync method. So I can't get any response or HTTP Status Code even this URL actually works in the browser. I am desperate to find out how to handle this case. If the URL

Equivalent of “curl -F” parameter for System.Net.Http.MultipartFormDataContent?

扶醉桌前 提交于 2019-12-05 11:57:09
I am trying to use the sonicAPI file/upload API in C#. My attempt to translate the curl example to C# with HttpClient and MultipartFormDataContent returns the error 400 / Bad Request . Content of the response : <?xml version="1.0" encoding="UTF-8"?> <response> <status code="400" /> <errors> <error message="File upload failed: file is missing." parameter="file" error_code="10" /> </errors> </response> Example of the curl command-line shown in the documentation : curl https://api.sonicapi.com/file/upload?access_id=$ACCESS_ID -Ffile=@Vocals.mp3 Code I've crafted so far : public async Task

Re-using HttpClient but with a different Timeout setting per request?

三世轮回 提交于 2019-12-05 09:13:51
问题 In order to reuse open TCP connections with HttpClient you have to share a single instance for all requests. This means that we cannot simply instantiate HttpClient with different settings (e.g. timeout or headers). How can we share the connections and use different settings at the same time? This was very easy, in fact the default, with the older HttpWebRequest and WebClient infrastructure. Note, that simply setting HttpClient.Timeout before making a request is not thread safe and would not

Difference between HttpClient and Unity's UnityWebRequest/WWW API

血红的双手。 提交于 2019-12-05 09:13:31
Unity now supports .NET 4.5 and I'm able to use the HttpClient class from the System.Net.Http namespace. That is great since I have existing client libraries which are therefore easy to reuse. Does anyone know the internal difference between using the mono HttpClient and Unity's own networking classes? I'm afraid that the HttpClient is not as optimized for the different platforms and might therefore cause trouble or be slower. Programmer There are many differences between the two. UnityWebRequest won't allow you to use change some headers. With HttpClient , you can change almost any header.

Creating jira issue via Rest c# httpClient

落爺英雄遲暮 提交于 2019-12-05 09:11:43
I have read one answer on atlassian https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response where one user created a JIRA issue by the following code. I adapted it but get an error by using a self-build class issue with ObjectContent Http.HttpContent content = new Http.ObjectContent<Issue>(data, jsonFormatter); The compiler wont accept it. Does anybody know why? public string CreateJiraIssue() { string data= @"{ ""fields"": { ""project"": { ""key"": ""HELP"" }, ""summary"": ""Test Ticket"", ""description"": ""Creating of

Disable AutoRedirect in FlurlClient

不想你离开。 提交于 2019-12-05 08:58:41
I am using FlurlHttp and I want to disable AllowAutoRedirect for some API calls. I know How can I get System.Net.Http.HttpClient to not follow 302 redirects? WebRequestHandler webRequestHandler = new WebRequestHandler(); webRequestHandler.AllowAutoRedirect = false; HttpClient httpClient = new HttpClient(webRequestHandler); // Send a request using GetAsync or PostAsync Task<HttpResponseMessage> response = httpClient.GetAsync("http://www.google.com") But for Flurl I found only the way similar to described in C# Flurl - Add WebRequestHandler to FlurlClient (I haven't compiled yet the code below ,

How to disable base64-encoded filenames in HttpClient/MultipartFormDataContent

。_饼干妹妹 提交于 2019-12-05 08:41:24
I'm using HttpClient to POST MultipartFormDataContent to a Java web application. I'm uploading several StringContents and one file which I add as a StreamContent using MultipartFormDataContent.Add(HttpContent content, String name, String fileName) using the method HttpClient.PostAsync(String, HttpContent) . This works fine, except when I provide a fileName that contains german umlauts (I haven't tested other non-ASCII characters yet). In this case, fileName is being base64-encoded. The result for a file named 99 2 LD 353 Temp Äüöß-1.txt looks like this: __utf-8_B