Programming

HttpContent 사용 방법을 찾을 수 없습니다

procodes 2020. 6. 2. 21:30
반응형

HttpContent 사용 방법을 찾을 수 없습니다


사용하려고합니다 HttpContent:

HttpContent myContent = HttpContent.Create(SOME_JSON);

...하지만 DLL이 정의 된 곳을 찾지 못했습니다.

첫째, 참조를 추가하는 시도 Microsoft.Http뿐만 아니라 System.Net,하지만 목록에 둘 수 없습니다. 또한 참조를 추가하려고 System.Net.Http했지만 HttpContent클래스를 사용할 수 없습니다.

HttpContent수업을 어디서 찾을 수 있습니까?


클래스 System.Net.Http 어셈블리 에있는 것으로 표시됩니다 . 이 클래스는 .NET 4.5의 새로운 기능이므로 해당 버전의 BCL을 사용해야합니다.


그냥 사용하십시오 ...

var stringContent = new StringContent(jObject.ToString());
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

또는,

var stringContent = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

6footunder의 의견을 받아 답변으로 바꾸려면 HttpContent추상적이므로 파생 클래스 중 하나를 사용해야합니다.

여기에 이미지 설명을 입력하십시오


JSON Post의 경우 :

var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

비 JSON :

var stringContent = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("field1", "value1"),
    new KeyValuePair<string, string>("field2", "value2"),
});
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

https://blog.pedrofelix.org/2012/01/16/the-new-system-net-http-classes-message-content/


While the final version of HttpContent and the entire System.Net.Http namespace will come with .NET 4.5, you can use a .NET 4 version by adding the Microsoft.Net.Http package from NuGet


I'm pretty sure the code is not using the System.Net.Http.HttpContent class, but instead Microsoft.Http.HttpContent. Microsoft.Http was the WCF REST Starter Kit, which never made it out preview before being placed in the .NET Framework. You can still find it here: http://aspnet.codeplex.com/releases/view/24644

I would not recommend basing new code on it.


The System.Net.Http namespace (where HttpContent class resides) is new to .Net 4.5, are you using a VS2012 RC?

그렇지 않으면, 당신은 이것에 액세스 할 수 없습니다.

참고 URL : https://stackoverflow.com/questions/11145053/cant-find-how-to-use-httpcontent

반응형