dotnet-httpclient

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

最后都变了- 提交于 2019-12-12 09:46:53
问题 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"); //

C# HttpClient.SendAsync always returns 404 but URL works in browser

北城以北 提交于 2019-12-12 09:36:23
问题 I am developing an C# console application for testing whether a URL is valid or not. It works well for most of URLs. But we found that there are some cases the application always got 404 response from target site but the URLs actually work in the browser. And those URLs also works when I tried them in the tools such as DHC (Dev HTTP Client). In the beginning, I though that this could be the reason of not adding right headers. But after tried using Fiddler to compose a http request with same

Object is disposed after PostAsync with HttpClient

人走茶凉 提交于 2019-12-12 08:45:34
问题 I'm trying to send a file with HttpClient and if something on the receiving side fails I want to resend the same file stream. I'm creating a post request with a MultipartFormDataContent, which contains the stream. Everything looks fine when I call PostAsync for the first time. But when I try to repeat the request I get System.ObjectDisposedException. My file stream is disposed after the first call of PostAsync... Why and is there a solution to my problem? Here is basic example of what am I

SocketException (0x274c) while accessing service with HttpClient

早过忘川 提交于 2019-12-12 06:39:00
问题 I have a httpClient code piece in this piece of code I requested (PostAsync) 3 times to the service like; private async Task<Data> SendRequestToTheService(string searchTerm) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(baseUrl); Response response = new Response(); await Step1(param1, client); await Step2(param1, param2, client); response = await Step3(param, client); return response; } } As you can see inside of the steps I send a request( postAsync ) to the same

How to preserve headers in the Flurl HttpClient

自古美人都是妖i 提交于 2019-12-12 04:56:52
问题 I'm using the Furl.Http wrapper over the .NET Http Client. With each request my API requires a User-Agent and an Authorization header to be sent. I would like to set that up once, rather than having to state that every time. What I thought I would be able to do is create an instance of the FlurlClient and set the headers on it, then ResetToRoot before each request, as illustrated in this sample piece of code: var fc = new FlurlClient(); fc.WithHeader("User-Agent", "Keep/1.0"); var tokenModel

Common Login for multiple WinRT apps

萝らか妹 提交于 2019-12-12 02:31:58
问题 I have two WinRT apps which request same Login service for a session. There is a global HttpClient(along with a cookiecontainer) in each of the apps which saves the cookie for further requests. Currently if a user logs into app A and opens App B, the user has to login again(Even if the session has not timed out). I do not want the user to login again if he has logged into one app. How can I achieve this? How can I share this HttpClient object across apps? Edit: Sessions are being maintained

Threading issues when using HttpClient for asynchronous file downloads

我怕爱的太早我们不能终老 提交于 2019-12-12 01:55:33
问题 This question is a follow-up to Using HttpClient for Asynchronous File downloads. 2015/01/15 Edited to add in accomodation for multithreading - still have a mystery, using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading.Tasks; namespace TestHttpClient2 { class Program { /* Use Yahoo portal to access quotes for stocks - perform asynchronous operations. */ static string baseUrl = "http://real-chart.finance.yahoo.com/"; static string

Win Phone 8 / Asp .Net Web API Image Upload

怎甘沉沦 提交于 2019-12-11 22:20:12
问题 The following code responsible for uploading images: [HttpPost] public async Task<HttpResponseMessage> Upload() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var streamProvider = new MultipartMemoryStreamProvider(); Cloudinary cloudinary = new Cloudinary(ConfigurationManager.AppSettings.Get("CLOUDINARY_URL")); return await Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t => { if (t.IsFaulted || t

Web API - Converting JSON to Complex Object Type (DTO) with inheritance

爷,独闯天下 提交于 2019-12-11 21:26:21
问题 I have the following scenario: public class WidgetBaseDTO { public int WidgetID { get; set; } } public class WidgetTypeA : WidgetBaseDTO { public string SomeProperty1 { get; set; } } public class WidgetTypeB : WidgetBaseDTO { public int SomeProperty2 { get; set; } } and my web service returns the following dashboard object whereas the Widgets collection could be of either type A or B: public class DashboardDTO { public List<WidgetBaseDTO> Widgets { get; set; } } my problem is that although

Unit testing Web Service responses

风流意气都作罢 提交于 2019-12-11 19:54:12
问题 I am currently writing an API wrapper in C# for ResellerClub's REST/HTTP API, which provides responses in garden-variety JSON objects. Invocation is performed by performing HTTP POST/GET on API endpoints using the HttpClient class. JSON.Net is used for parsing the responses. How I can unit test my API wrapper functionality for the API as most calls require a level of expected state in order to succeed. For example, I cannot test the creation of a CNAME record on a domain that I have not