dotnet-httpclient

Post (or put) request with uri parameter not working when calling with HttpClient

☆樱花仙子☆ 提交于 2020-01-06 08:03:33
问题 In trying to make a Web API in c# and consume it via c# HttpClient, I've already written some controllers and client calls and is working fine, but now I need to call a Post method with two parameters, one simple value (long) directly in the URL[FromURI], and the another one is a complex type passed in body. I think I can simplify the answer, suppose I just want to call a post or put with one simple parameter passed in the URL. First of all, lets see my web server mapping config:

Refactoring code to allow for unit testing of HttpClient

…衆ロ難τιáo~ 提交于 2020-01-06 06:38:38
问题 I am dealing with a piece of code that looks like this: public class Uploader : IUploader { public Uploader() { // assign member variables to dependency injected interface implementations } public async Task<string> Upload(string url, string data) { HttpResponseMessage result; try { var handler = new HttpClientHandler(); var client = new HttpClient(handler); result = await client.PostAsync(url, new FormUrlEncodedContent(data)); if (result.StatusCode != HttpStatusCode.OK) { return "Some Error

HttpClient Portable returns 404 notfound on WP8

一世执手 提交于 2020-01-06 03:17:12
问题 I'm porting a W8 application that uses httpclient library to connect to our server. The main purpose of the application is to send images, but when I try to send pictures on my WP8 I got a 404 not found error (seems that Microsoft remapped to 404 a lot of errors), if i check the server logs, I can see that the server recevied about 1/4 of the image before failling. The same function seems to works fine in my W8 application (didn't tested on 3G), and works on WP8 if I use Wifi connection. I

Logging into Salesforce with C# HttpClient

六月ゝ 毕业季﹏ 提交于 2020-01-05 04:38:16
问题 I can't seem to get this code to work: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpClient client = new HttpClient(); client.BaseAddress = new Uri("https://login.salesforce.com"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); client.DefaultRequestHeaders.Add("User-Agent", "SFAPIClient"); //Random Client var parameters = new Dictionary<string, string>(); parameters["grant_type"] = "password";

httpclient reuse in mvc.net core web app with multiple apis and querystrings

大城市里の小女人 提交于 2020-01-04 09:14:14
问题 I've read a lot of conflicting answers about this and still don't have it straight. I have a .net core 1.1 web app that hits a lot of external api's for various data sources. My curent implementation uses DI to pass a custom "webclient" class to each controller, which internally creates and disposes a httpclient for each request (inside a using block). I see a ton of idle connections on the server so I'm pretty sure this is causing these. I also have some issues with the .net core app

Getting HttpClient to work with Kerberos

天大地大妈咪最大 提交于 2020-01-04 06:06:30
问题 I'm building an application in an environment that's configured with Kerberos authentication. My application uses .NET's HttpClient to POST some data to an API hosted in IIS on a different server. GET requests seem to be working fine, but POSTs to the same service instantly implode with the following exception: System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: The read operation failed, see inner exception. ---> System.Net.WebException:

Getting HttpClient to work with Kerberos

扶醉桌前 提交于 2020-01-04 06:05:20
问题 I'm building an application in an environment that's configured with Kerberos authentication. My application uses .NET's HttpClient to POST some data to an API hosted in IIS on a different server. GET requests seem to be working fine, but POSTs to the same service instantly implode with the following exception: System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: The read operation failed, see inner exception. ---> System.Net.WebException:

Using a custom value for HttpMethod

烈酒焚心 提交于 2020-01-03 15:22:26
问题 I'm using the HttpClient and I need to set a non-standard type for the HttpMethod . Where using HttpWebRequest only expects a string, HttpClient expects an HttpMethod . Enumerating the available values in HttpMethod , I don't see a way to add a custom one. Any thoughts? 回答1: Don't know why I didn't think of trying this before, but I can call new HttpMethod("MYMETHOD"); 回答2: Thank you so much. I tried the weirdest stuff, but i did not see this simple solution :) I try to use CalDAV REST

How to Configure Network Tracing Dotnet core for HttpClient calls?

人走茶凉 提交于 2020-01-02 09:29:15
问题 As per reference document at https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-configure-network-tracing We can setup this in web.config or any other configuration file and we get detailed system.net traces, packets traces for HttpClient calls and any kind of issue in HttpClient calls can be captured in traces, be it certificate, TLS or anything else. However, do we have similar implementation for dotnet core / standard which can be used in both web app or console

Refresh Token using Static HttpClient

不羁的心 提交于 2020-01-02 06:36:05
问题 Using VS 2017 .Net 4.5.2 I have the following class public static class MyHttpClient { //fields private static Lazy<Task<HttpClient>> _Client = new Lazy<Task<HttpClient>>(async () => { var client = new HttpClient(); await InitClient(client).ConfigureAwait(false); return client; }); //properties public static Task<HttpClient> ClientTask => _Client.Value; //methods private static async Task InitClient(HttpClient client) { //resey headers client.DefaultRequestHeaders.Clear(); //Set base URL, NOT