dotnet-httpclient

HttpClient failing in accessing simple website

▼魔方 西西 提交于 2019-12-13 05:15:25
问题 Here is my code internal static void ValidateUrl(string url) { Uri validUri; if(Uri.TryCreate(url,UriKind.Absolute,out validUri)) { using (HttpClient client = new HttpClient()) { try { HttpResponseMessage response = client.Get(url); response.EnsureStatusIsSuccessful(); } catch (Exception ex) { //exception handler goes here } } } } This code when i run it produces this result. ProxyAuthenticationRequired (407) is not one of the following: OK (200), Created (201), Accepted (202),

How to get OAth Token with HttpClient?

一笑奈何 提交于 2019-12-13 03:36:13
问题 I need to get a token for a REST call. The token should be in a JSON result. https://github.com/bic-boxtech/BIC-BoxTech-API-Samples/wiki/Authentication Here is the way I tried it: private async void GetBicDataAsync() { HttpClient _bicAothClient; _bicAothClient = new HttpClient(); _bicAothClient.DefaultRequestHeaders.Add("Authorization", "Basic YmljYXBwOmJpY3NlY3JldGFwcA=="); var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("grant_type", "password"), new

Cannot deserialize the current JSON object xamarin.forms

倾然丶 夕夏残阳落幕 提交于 2019-12-13 02:41:44
问题 Im using xamarin.forms to create an app that uses the MusixMatch api. It's throwing the following exception : Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List. To my knowledge, Ive done everything correctly, not sure why this exception is being thrown. Any help will be appreciated. TrackList.cs public class TrackList { public class Track { public int track_id { get; set; } public string track_mbid { get; set; } public string

WriteableBitmap SaveJpeg missing for universal apps

拟墨画扇 提交于 2019-12-12 17:37:49
问题 I am developing an universal app, in my shared code i am trying to download the image from net and save the image to LocalFolder. I am using HttpClient to download the images from user given urls and reading the client response to save the image. I am using below code to save, but couldn't able to find Writeable SaveJpeg method. HttpResponseMessage response = await httpClient.GetAsync(imageUri); await Task.Run(async () => { if (response.IsSuccessStatusCode) { // save image locally

Using a Keep-Alive connection in WinRT's HttpClient class?

我怕爱的太早我们不能终老 提交于 2019-12-12 16:05:27
问题 Our WinRT app is incredibly slow when opening connections to our servers. Requests take ~500ms to run. This is blocking some of our scenarios. When debugging, we noticed that when Fiddler is active, the requests are much faster - ~100ms per request. Some searches later we understood that was because Fiddler was using Keep-Alive connections when proxying calls, which makes our proxied calls much faster. We double-checked this in two ways. We set UseProxy to false and observed that the request

How to disable pipelining for the .NET HttpClient

风流意气都作罢 提交于 2019-12-12 12:35:04
问题 As the title says I'd like to disable the pipelining functionality BUT still make use of the connection pool (so KeepAlive=False is not an option). The reason is that longer requests may prevent shorter requests from executing fast, but opening up a new connection for every requests is significantly slower. I'm doing this: _webRequestHandler = new WebRequestHandler(); _webRequestHandler.AllowPipelining = false; _httpClient = new HttpClient(_webRequestHandler); await _httpClient.SendAsync

Saving an image from the web to the Saved Pictures folder in Windows Phone 8.1 RT C#

自古美人都是妖i 提交于 2019-12-12 12:27:20
问题 How can I save an image to the Saved Pictures folder in Windows Phone 8.1 RT? I downloaded the image from the web using HttpClient. 回答1: You just need this var url = "Some URL"; var fileName = Path.GetFileName(url.LocalPath); var thumbnail = RandomAccessStreamReference.CreateFromUri(url); var remoteFile = await StorageFile.CreateStreamedFileFromUriAsync(fileName, url, thumbnail); await remoteFile.CopyAsync(KnownFolders.SavedPictures, fileName, NameCollisionOption.GenerateUniqueName); You need

Consuming xml using HttpClient from webapi

三世轮回 提交于 2019-12-12 11:04:47
问题 I used WebClient to get an Xml object from a restfull service (.net web api) and everything worked great: using(WebClient client = new WebClient()) { client.Encoding = UTF8Encoding.UTF8; client.Headers[HttpRequestHeader.ContentType] = "text/xml"; client.Credentials = // ....; xmlResult = webClient.DownloadString(url); } .... this code works great. I get an Xml as a string back, everyone's happy. Now, I changed it so it would work with HttpClient instead and I can't get an Xml returned -

HttpClient hangs when timeout is setting (Windows Phone)

╄→尐↘猪︶ㄣ 提交于 2019-12-12 10:43:54
问题 I'm trying to set timeout to HttpClient object in Windows Phone App. But if request is not completed before timeout, GetAsync never returns a value. I'm using the following code to get response: HttpClientHandler handler = new HttpClientHandler(); HttpClient client = new HttpClient(handler); client.Timeout = TimeSpan.FromSeconds(5); client.BaseAddress = new Uri("http://www.foo.com"); HttpResponseMessage response = await client.GetAsync("/boo.mp3");//<--Hangs byte[] data = await response

Why can't I catch exception from HttpClient SendAsync when using ObjectContent

允我心安 提交于 2019-12-12 10:42:19
问题 I can't seem to catch certain exceptions thrown from the System.Net.Http.HttpClient's SendAsync method. Specifically, the ProtocolViolationException, thrown from the HttpWebRequest.ChechProtocol method. The code seems to deadlock or hang, and the catch is never entered. I've created an NUnit unit test to illustrate the issue clearly. public class TestClass { public bool TestProperty { get; set; } } [Test] public async Task CanCatchExceptionFromHttpClientSendAsync() { var caughtException =