通过 Web 客户端 post 方法 C# 传递两个 JSON 对象
Pass two JSON object by web client post method C#
我需要将以下对象作为参数传递给使用 C# 中的 Web 客户端方法的 post 方法。
{
"company":
{
"id": "e63dfcab345260b2591f585126ede56627db4ef2"
},
"requestor":
{
"id": "",
"email": "customer@example.com ",
"firstName": "Test",
"lastName": "Requestor",
"role": "employerAdmin",
"phone": "(415)1112222",
"title": "HR Manager"
}
}
我是这样转换的
company[id]=e63dfcab345260b2591f585126ede56627db4ef2&requestor[id]=&requestor[email]=customer@example.com+&requestor[firstName]=Test&requestor[lastName]=Requestor&requestor[role]=employerAdmin&requestor[phone]=(415)1112222&requestor[title]=HR+Manager
但是我收到无效参数错误。请帮我。提前致谢。
我的全部代码如下:
using (var Requestor = new System.Net.WebClient())
{
string url = "https://stormaas-pre.inflection.com:8443/v1/Requestor?";
string parameters = "company[id]='757563a3-67df-4a6e-9ef9-d89d57d41e0d'&requestor[id]=''&requestor[email]='customer@example.com'&requestor[firstName]='Test'&requestor[lastName]='Requestor'&requestor[role]='employerAdmin'&requestor[phone]='(415)1112222'&requestor[title]='HRManager'";
Requestor.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Requestor.Credentials = new System.Net.NetworkCredential("xyz:xyz!", "");
Requestor.Encoding = System.Text.Encoding.UTF8;
Requestor.Headers[HttpRequestHeader.ContentType] = "application/json";
Requestor.Headers[HttpRequestHeader.Accept] = "text/xml";
string res = Requestor.UploadString(url, "POST", parameters);
}
您收到无效参数错误,因为 url 中不允许使用方括号。如果要使用 RFC 1738 you must use HttpUtility.UrlEncode("String")
(msdn reference)
中未包含的字符
您可以为请求者创建 POCO 类,公司如下所示,并使用 JSON.net 为您进行转换。
public class Company
{
public string Id { get; set; }
// Other properties
}
public class Requestor
{
public string Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// Other properties
}
public class Container
{
public Company Company { get; set; }
public Requestor Requestor { get; set; }
}
var requestor = new Container();
requestor.Company = new Company { Id = "sampleid" };
requestor.Requestor = new Requestor
{
FirstName = "test",
LastName = "testname"
};
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var data = JsonConvert.SerializeObject(requestor, settings);
WebClient client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
// Code for the credentials etc
client.UploadString(@"your url", data);
希望这对您有所帮助。为此,您需要引用 JSON.net。由于您使用的是 .net 4.5 和 Web API,您应该已经有了参考。
我需要将以下对象作为参数传递给使用 C# 中的 Web 客户端方法的 post 方法。
{
"company":
{
"id": "e63dfcab345260b2591f585126ede56627db4ef2"
},
"requestor":
{
"id": "",
"email": "customer@example.com ",
"firstName": "Test",
"lastName": "Requestor",
"role": "employerAdmin",
"phone": "(415)1112222",
"title": "HR Manager"
}
}
我是这样转换的
company[id]=e63dfcab345260b2591f585126ede56627db4ef2&requestor[id]=&requestor[email]=customer@example.com+&requestor[firstName]=Test&requestor[lastName]=Requestor&requestor[role]=employerAdmin&requestor[phone]=(415)1112222&requestor[title]=HR+Manager
但是我收到无效参数错误。请帮我。提前致谢。
我的全部代码如下:
using (var Requestor = new System.Net.WebClient())
{
string url = "https://stormaas-pre.inflection.com:8443/v1/Requestor?";
string parameters = "company[id]='757563a3-67df-4a6e-9ef9-d89d57d41e0d'&requestor[id]=''&requestor[email]='customer@example.com'&requestor[firstName]='Test'&requestor[lastName]='Requestor'&requestor[role]='employerAdmin'&requestor[phone]='(415)1112222'&requestor[title]='HRManager'";
Requestor.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Requestor.Credentials = new System.Net.NetworkCredential("xyz:xyz!", "");
Requestor.Encoding = System.Text.Encoding.UTF8;
Requestor.Headers[HttpRequestHeader.ContentType] = "application/json";
Requestor.Headers[HttpRequestHeader.Accept] = "text/xml";
string res = Requestor.UploadString(url, "POST", parameters);
}
您收到无效参数错误,因为 url 中不允许使用方括号。如果要使用 RFC 1738 you must use HttpUtility.UrlEncode("String")
(msdn reference)
您可以为请求者创建 POCO 类,公司如下所示,并使用 JSON.net 为您进行转换。
public class Company
{
public string Id { get; set; }
// Other properties
}
public class Requestor
{
public string Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// Other properties
}
public class Container
{
public Company Company { get; set; }
public Requestor Requestor { get; set; }
}
var requestor = new Container();
requestor.Company = new Company { Id = "sampleid" };
requestor.Requestor = new Requestor
{
FirstName = "test",
LastName = "testname"
};
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var data = JsonConvert.SerializeObject(requestor, settings);
WebClient client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
// Code for the credentials etc
client.UploadString(@"your url", data);
希望这对您有所帮助。为此,您需要引用 JSON.net。由于您使用的是 .net 4.5 和 Web API,您应该已经有了参考。