如何使用C#将此数据发布到webservice(JSON数据)

时间:2015-06-11 16:44:41

标签: c# json

这是python代码:(我需要在C#中使用它)。

import requests #requires "requests" package
import json
response = requests.post('https://scm.commerceinterface.com/api/v2/mark_exported', 
                       data={'supplier_id':'1', 'token':'xYRPKcoakMoiRzWgKLV5TqPSdNAaZQT', 
                             'ci_lineitem_ids':json.dumps([54553919, 54553920])}).json()
if response['success'] == True:
   #Successfully marked as exported (only items which are not already marked exported)
   pass
else:
   pass

如何根据需要格式化字符串,我是否缺少必要的代码?我目前无法用实际数据测试代码。

到目前为止,这是C#代码:

public void postResponse(string supplierid, string token, string geturl, string lineid)
{

    try
    {
        lineid = lineid.Trim();
         string postdata = ("{'supplier_id':'"+supplierid+"','token':'"+token+"','ci_lineitem_ids':["+lineid+"]}");
        WebClient postWithParamsClient = new WebClient();
        postWithParamsClient.Headers.Add("Content-Type", "application/json");
        postWithParamsClient.Headers["Content-Length"] = postdata.Length.ToString();
        postWithParamsClient.UploadStringAsync(new Uri(geturl),
                                                   "POST",
                                                   postdata);
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}

0 个答案:

没有答案