通过Web客户端POST方法C#传递两个JSON对象 [英] Pass two JSON object by web client post method C#

查看:484
本文介绍了通过Web客户端POST方法C#传递两个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下对象传递作为使用Web客户端的方法在C#中的POST方法的参数。

  {
公司:
{
标识:e63dfcab345260b2591f585126ede56627db4ef2
},
请求者:
{
标识:,
电子邮件:customer@example.com,
名字:测试,
姓氏:请求,
角色:employerAdmin,
手机:(415)1112222,
头衔:人力资源经理
}
}

我转换像这种

 公司[ID] = e63dfcab345260b2591f585126ede56627db4ef2&放大器;请求者[ID] =安培;请求者[邮件] =customer@example.com+ &安培;请求者[名字] =测试与放大器;请求者[姓氏] =请求者放大器;请求者[作用] = employerAdmin&放大器;请求者[电话] =(415)1112222&安培;请求者[标题] = HR +经理

但我正在逐渐无效的参数错误。请帮帮我。 。在此先感谢



我的整个代码如下:

 使用(VAR请求=新System.Net.WebClient())
{

字符串URL =?https://stormaas-pre.inflection.com:8443/v1/Requestor
字符串参数=公司[ID] ='757563a3-67df-4a6e-9ef9-d89d57d41e0d'和;请求[ID] =''和;请求[邮件] ='customer@example.com'&请求者[名字] ='测试'和;请求[姓氏] ='请求'和;请求[作用] ='employerAdmin'和;请求[手机] ='(415)1112222'和;请求[标题] ='HRMANAGER
Requestor.Headers.Add(用户代理,Mozilla的/ 4.0(兼容; MSIE 6.0; Windows NT的5.2; .NET CLR 1.0.3705;));
Requestor.Credentials =新System.Net.NetworkCredential(XYZ:XYZ!,);
Requestor.Encoding = System.Text.Encoding.UTF8;
Requestor.Headers [HttpRequestHeader.ContentType] =应用/ JSON;
Requestor.Headers [HttpRequestHeader.Accept] =文/ XML;
串解析度= Requestor.UploadString(URL,POST,参数);
}


解决方案

您可以创建POCO类的请求,公司遵循一样,用JSON.net为你做转换。

 公共类公司
{
公共字符串ID {搞定;组; }
//其它属性
}

公共类请求
{
公共字符串ID {搞定;组; }

公共字符串电子邮件{获得;组; }

公共字符串名字{获得;组; }

公共字符串名字{获得;组; }
//其它属性
}

公共类集装箱
{
上市公司的公司{搞定;组; }

公共请求请求{搞定;组; }
}

变种请求者=新的Container();
requestor.Company =新公司{ID =sampleid};
requestor.Requestor =新请求
{
名字=测试,
姓氏=测试名
};

JsonSerializerSettings设置=新JsonSerializerSettings();
settings.ContractResolver =新CamelCasePropertyNamesContractResolver();
VAR数据= JsonConvert.SerializeObject(请求者,设置);

WebClient的客户端=新的WebClient();
client.Headers.Add(HttpRequestHeader.ContentType,应用/ JSON);
//代码凭证等
client.UploadString(@你的URL,数据);



希望这有助于。对于这个工作,你需要有一个JSON.net参考。由于您使用的.NET 4.5使用Web API,你应该已经具有参考。


I need to pass below object as a parameter for a post method using web client method in C#.

     {
    "company":
    {
        "id": "e63dfcab345260b2591f585126ede56627db4ef2"
    },
    "requestor":
    {
        "id": "",
        "email": "customer@example.com ",
        "firstName": "Test",
        "lastName": "Requestor",
        "role": "employerAdmin",
        "phone": "(415)1112222",
        "title": "HR Manager"
    }
}

I converted like this

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

But i am getting invalid parameter error. Please help me. Thanks in advance.

My entire code is below:

 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);
            }

解决方案

You can create POCO classes for the Requestor, Company like follows and use JSON.net to do the conversion for you.

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);

Hope this helps. For this to work, you need to have a reference to JSON.net. Since you are using .net 4.5 with Web API, you should be having the reference already.

这篇关于通过Web客户端POST方法C#传递两个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆