This question already has an answer here:
这个问题在这里已有答案:
- How to post JSON to a server using C#? 11 answers
如何使用C#将JSON发布到服务器? 11个答案
I want to send json data in POST request using C#.
我想使用C#在POST请求中发送json数据。
I have tried few ways but facing lot of issues . I need to request using request body as raw json from string and json data from json file.
我尝试了很多方法,但面临很多问题。我需要请求使用请求体作为来自字符串的原始json和来自json文件的json数据。
How can i send request using these two data forms.
如何使用这两个数据表单发送请求。
Ex: For authentication request body in json --> {"Username":"myusername","Password":"pass"}
例如:对于json中的身份验证请求正文 - > {“Username”:“myusername”,“Password”:“pass”}
For other APIs request body should retrieved from external json file.
对于其他API请求体应从外部json文件中检索。
4 个解决方案
#1
12
You can use the HttpClient
instead of the WebClient
and HttpWebRequest
. It's a newer implementation.
您可以使用HttpClient而不是WebClient和HttpWebRequest。这是一个较新的实现。
string myJson = "{'Username': 'myusername','Password':'pass'}";
using (var client = new HttpClient())
{
var response = await client.PostAsync(
"http://yourUrl",
new StringContent(myJson, Encoding.UTF8, "application/json"));
}
When you need your HttpClient
more then once it's recommended to only create one instance and reuse it or use the new HttpClientFactory
.
当你需要更多的HttpClient时,建议只创建一个实例并重用它或使用新的HttpClientFactory。
#2
5
You can do it with HttpWebRequest
:
你可以用HttpWebRequest做到这一点:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://yourUrl");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
Username = "myusername",
Password = "pass"
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
#3
2
You can use either HttpClient
or RestSharp
, since i do not know whats ur code here is an example using httpclient :
你可以使用HttpClient或RestSharp,因为我不知道你的代码是什么,这是一个使用httpclient的例子:
using (var client = new HttpClient())
{
//This would be the like http://www.uber.com
client.BaseAddress = new Uri("Base Address/URL Address");
//serialize your json using newtonsoft json serializer then add it to the StringContent
var content = new StringContent(YourJson, Encoding.UTF8, "application/json")
//method address would be like api/callUber:SomePort for example
var result = await client.PostAsync("Method Address", content);
string resultContent = await result.Content.ReadAsStringAsync();
}
#4
1
This works for me.
这对我有用。
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new
StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
Username = "myusername",
Password = "password"
});
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
#1
12
You can use the HttpClient
instead of the WebClient
and HttpWebRequest
. It's a newer implementation.
您可以使用HttpClient而不是WebClient和HttpWebRequest。这是一个较新的实现。
string myJson = "{'Username': 'myusername','Password':'pass'}";
using (var client = new HttpClient())
{
var response = await client.PostAsync(
"http://yourUrl",
new StringContent(myJson, Encoding.UTF8, "application/json"));
}
When you need your HttpClient
more then once it's recommended to only create one instance and reuse it or use the new HttpClientFactory
.
当你需要更多的HttpClient时,建议只创建一个实例并重用它或使用新的HttpClientFactory。
#2
5
You can do it with HttpWebRequest
:
你可以用HttpWebRequest做到这一点:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://yourUrl");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
Username = "myusername",
Password = "pass"
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
#3
2
You can use either HttpClient
or RestSharp
, since i do not know whats ur code here is an example using httpclient :
你可以使用HttpClient或RestSharp,因为我不知道你的代码是什么,这是一个使用httpclient的例子:
using (var client = new HttpClient())
{
//This would be the like http://www.uber.com
client.BaseAddress = new Uri("Base Address/URL Address");
//serialize your json using newtonsoft json serializer then add it to the StringContent
var content = new StringContent(YourJson, Encoding.UTF8, "application/json")
//method address would be like api/callUber:SomePort for example
var result = await client.PostAsync("Method Address", content);
string resultContent = await result.Content.ReadAsStringAsync();
}
#4
1
This works for me.
这对我有用。
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new
StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
Username = "myusername",
Password = "password"
});
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}