如何postjson进入HTTP服务器
How to post json into HTTP server
我想知道如何将 post 从 json 发送到 http 服务器。
我用来执行 json 的代码如下:
pedro product = new pedro();
product.FirtsName = "Ola";
product.ID = 1;
product.idade= 10;
string json = JsonConvert.SerializeObject(product);
这是 pedro
class:
public class pedro
{
public int ID { get; set; }
public string FirtsName { get; set; }
public int idade { get; set; }
}
对于 WebApi,您可以这样使用:
string url = "http://url.of.server/";
Pedro product = new Pedro();
product.FirtsName = "Ola";
product.ID = 1;
product.Idade = 10;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync(url, product).Result;
if (response.IsSuccessStatusCode)
{
// do something
}
}
如果您不使用 WepApi,有许多类似的方法,例如:https://whosebug.com/a/39414248/7489072
不要按照评论中的建议对 post 的 body 进行 Base64 编码,除非您绝对必须/想要 post 二进制文件并控制接收网络服务器。在 99% 的情况下,Web 服务器期望纯文本 body。
如果需要post ASCII范围外的字符,使用正确的HTTPheaders指定Unicodebody加载。
更新 1 (headers):
HttpClient class 有 属性 DefaultRequestHeaders 可用于设置常见请求 headers,例如 AcceptEncoding。如果您需要对内容 headers 进行更细粒度的控制,请使用 .PostAsync(string uri, HttpContent content) 而不是 .PostAsJsonAsync(这只是为设置一些默认值 headers Json 内容)
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string stringFromObject = JsonConvert.SerializeObject(product);
HttpContent content = new StringContent(stringFromObject, Encoding.UTF8, "application/json");
content.Headers.Add("YourCustomHeader", "YourParameter");
HttpResponseMessage response = client.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
// do something
}
}
更新2(编码):
详细说明编码注释:当然你应该转义引号之类的。但这是 Json 标准的一部分,应该由通用编码器/解码器处理。最重要的是,您可以对序列化 object 的属性使用任何进一步的编码。例如 HTML-encoding 字符串和 Base64 二进制属性。只要您知道接收它的网络服务器就会正确解码。
{
"id": 3,
"title": "Decode this",
"description": "this is < HTML encoded >",
"profileImgBase64": "Nzg5MzQ4IHdleWhmQVMmKihFJiphc3R5WUdkdCphc14qVHlpZg0K"
}
因此对单个属性进行编码,但不要对整个 Json 有效负载进行编码,因为您必须在接收管道的开头对其进行解码,而这不是网络服务器能够理解的。
我想知道如何将 post 从 json 发送到 http 服务器。 我用来执行 json 的代码如下:
pedro product = new pedro();
product.FirtsName = "Ola";
product.ID = 1;
product.idade= 10;
string json = JsonConvert.SerializeObject(product);
这是 pedro
class:
public class pedro
{
public int ID { get; set; }
public string FirtsName { get; set; }
public int idade { get; set; }
}
对于 WebApi,您可以这样使用:
string url = "http://url.of.server/";
Pedro product = new Pedro();
product.FirtsName = "Ola";
product.ID = 1;
product.Idade = 10;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync(url, product).Result;
if (response.IsSuccessStatusCode)
{
// do something
}
}
如果您不使用 WepApi,有许多类似的方法,例如:https://whosebug.com/a/39414248/7489072
不要按照评论中的建议对 post 的 body 进行 Base64 编码,除非您绝对必须/想要 post 二进制文件并控制接收网络服务器。在 99% 的情况下,Web 服务器期望纯文本 body。 如果需要post ASCII范围外的字符,使用正确的HTTPheaders指定Unicodebody加载。
更新 1 (headers): HttpClient class 有 属性 DefaultRequestHeaders 可用于设置常见请求 headers,例如 AcceptEncoding。如果您需要对内容 headers 进行更细粒度的控制,请使用 .PostAsync(string uri, HttpContent content) 而不是 .PostAsJsonAsync(这只是为设置一些默认值 headers Json 内容)
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string stringFromObject = JsonConvert.SerializeObject(product);
HttpContent content = new StringContent(stringFromObject, Encoding.UTF8, "application/json");
content.Headers.Add("YourCustomHeader", "YourParameter");
HttpResponseMessage response = client.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
// do something
}
}
更新2(编码): 详细说明编码注释:当然你应该转义引号之类的。但这是 Json 标准的一部分,应该由通用编码器/解码器处理。最重要的是,您可以对序列化 object 的属性使用任何进一步的编码。例如 HTML-encoding 字符串和 Base64 二进制属性。只要您知道接收它的网络服务器就会正确解码。
{
"id": 3,
"title": "Decode this",
"description": "this is < HTML encoded >",
"profileImgBase64": "Nzg5MzQ4IHdleWhmQVMmKihFJiphc3R5WUdkdCphc14qVHlpZg0K"
}
因此对单个属性进行编码,但不要对整个 Json 有效负载进行编码,因为您必须在接收管道的开头对其进行解码,而这不是网络服务器能够理解的。