Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在给定以下HTTP+的情况下使用窗口服务发出POST请求;JSON_C#_Json_Rest_Post - Fatal编程技术网

C# 如何在给定以下HTTP+的情况下使用窗口服务发出POST请求;JSON

C# 如何在给定以下HTTP+的情况下使用窗口服务发出POST请求;JSON,c#,json,rest,post,C#,Json,Rest,Post,我正在尝试使用windows服务向发送post请求。workwave API示例提供的代码如下所示: POST /api/v1/territories/429defc8-5b05-4c3e-920d-0bb911a61345/orders HTTP/1.0 Accept: application/json X-WorkWave-Key: YOUR API KEY Host: wwrm.workwave.com Content-Type: application/json { "or

我正在尝试使用windows服务向发送post请求。workwave API示例提供的代码如下所示:

    POST /api/v1/territories/429defc8-5b05-4c3e-920d-0bb911a61345/orders HTTP/1.0
Accept: application/json
X-WorkWave-Key: YOUR API KEY
Host: wwrm.workwave.com
Content-Type: application/json

{
  "orders": [
    {
      "name": "Order 6 - API",
      "eligibility": {
        "type": "on",
        "onDates": [
          "20151204"
        ]
      },
      "forceVehicleId": null,
      "priority": 0,
      "loads": {
        "people": 2
      },
      "delivery": {
        "location": {
          "address": "2001 2nd Ave, Jasper, AL 35501, USA"
        },
        "timeWindows": [
          {
            "startSec": 43200,
            "endSec": 54000
          }
        ],
        "notes": "Order added via API",
        "serviceTimeSec": 1800,
        "tagsIn": [],
        "tagsOut": [],
        "customFields": {
          "my custom field": "custom field content",
          "orderId": "abcd1234"
        }
      }
    }
  ]
}

这是我第一次使用GET/POST请求。因此,我不确定上面的内容是什么,以及如何使用我的c#代码实现这一点。我需要遵循的步骤是什么,以及如何做到这一点。感谢您的时间和代码

您可以使用
HttpWebRequest
如下所示:

string PostJsonToGivenUrl(string url, object jsonObject)
    {
        string resultOfPost = string.Empty;

        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
        httpRequest.ContentType = "application/json";
        httpRequest.Method = "POST";

        using (StreamWriter writer = new StreamWriter(httpRequest.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(jsonObject);

            writer.Write(json);
        }

        HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            resultOfPost = streamReader.ReadToEnd();
        }

        return resultOfPost;
    }
如果您需要知道如何使用
JavaScriptSerializer
形成json字符串,请检查以下链接:

如果您需要有关
HttpWebRequest
的更多信息,请查看此链接:


另外,检查此答案可能会有所帮助:

您可以使用
HttpWebRequest
如下所示:

string PostJsonToGivenUrl(string url, object jsonObject)
    {
        string resultOfPost = string.Empty;

        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
        httpRequest.ContentType = "application/json";
        httpRequest.Method = "POST";

        using (StreamWriter writer = new StreamWriter(httpRequest.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(jsonObject);

            writer.Write(json);
        }

        HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            resultOfPost = streamReader.ReadToEnd();
        }

        return resultOfPost;
    }
如果您需要知道如何使用
JavaScriptSerializer
形成json字符串,请检查以下链接:

如果您需要有关
HttpWebRequest
的更多信息,请查看此链接:


另外,检查这个答案可能会有帮助:

首先,您需要创建请求和响应对象。您可以使用c#类创建者,比如json2charp.com

public class Eligibility
{
    public string type { get; set; }
    public List<string> onDates { get; set; }
}

public class Loads
{
    public int people { get; set; }
}

public class Location
{
    public string address { get; set; }
}

public class TimeWindow
{
    public int startSec { get; set; }
    public int endSec { get; set; }
}

public class CustomFields
{

    public string myCustomField { get; set; }
    public string orderId { get; set; }
}

public class Delivery
{
    public Location location { get; set; }
    public List<TimeWindow> timeWindows { get; set; }
    public string notes { get; set; }
    public int serviceTimeSec { get; set; }
    public List<object> tagsIn { get; set; }
    public List<object> tagsOut { get; set; }
    //this field will be Dictionary...
    public CustomFields customFields { get; set; }
}

public class Order
{
    public string name { get; set; }
    public Eligibility eligibility { get; set; }
    public object forceVehicleId { get; set; }
    public int priority { get; set; }
    public Loads loads { get; set; }
    public Delivery delivery { get; set; }
}

public class OrderRequest
{
    public List<Order> orders { get; set; }
}
public class OrderResponse
{
    public string requestId { get; set; }
}
公共类资格
{
公共字符串类型{get;set;}
公共列表日期{get;set;}
}
公共类负载
{
公共整数人{get;set;}
}
公共类位置
{
公共字符串地址{get;set;}
}
公共类时间窗口
{
public int startSec{get;set;}
公共int-endSec{get;set;}
}
公共类自定义字段
{
公共字符串myCustomField{get;set;}
公共字符串orderId{get;set;}
}
公共课堂教学
{
公共位置位置{get;set;}
公共列表时间窗口{get;set;}
公共字符串注释{get;set;}
公共int-serviceTimeSec{get;set;}
公共列表标记{get;set;}
公共列表标记sout{get;set;}
//此字段将是字典。。。
公共自定义字段自定义字段{get;set;}
}
公共阶级秩序
{
公共字符串名称{get;set;}
公共资格{get;set;}
公共对象forceVehicleId{get;set;}
公共int优先级{get;set;}
公共加载{get;set;}
公共传递{get;set;}
}
公共类OrderRequest
{
公共列表顺序{get;set;}
}
公共类OrderResponse
{
公共字符串requestId{get;set;}
}
并使用api引用示例值创建请求对象实例(C#)

OrderRequest GetOrderRequestObject()
    {
        var rootObj = new OrderRequest
        {
            orders = new List<Order>()
        };
        var order = new Order
        {
            name = "Order 6 - API",
            eligibility = new Eligibility
            {
                type = "on",
                onDates = new List<string>() { "20151204" }
            },
            forceVehicleId = null,
            priority = 2,
            loads = new Loads
            {
                people = 2
            },
            delivery = new Delivery
            {
                location = new Location
                {
                    address = "2001 2nd Ave, Jasper, AL 35501, USA"
                },
                timeWindows = new List<TimeWindow>(){
                     new TimeWindow{
                         startSec =43200,
                         endSec=54000
                     }},
                notes = "Order added via API",
                serviceTimeSec = 1800,
                tagsIn = new List<object>(),
                tagsOut = new List<object>(),
                customFields = new CustomFields
                {
                    myCustomField = "custom field content",
                    orderId = "abcd1234"
                }

            },
        };
        rootObj.orders.Add(order);
        return rootObj;
    }
OrderRequest GetOrderRequestObject()
{
var rootObj=新订单请求
{
订单=新列表()
};
var订单=新订单
{
name=“订单6-API”,
资格=新资格
{
type=“on”,
onDates=新列表(){“20151204”}
},
forceVehicleId=null,
优先级=2,
荷载=新荷载
{
人=2
},
交付=新交付
{
位置=新位置
{
地址=“2001美国阿拉巴马州贾斯珀第二大道,邮编35501”
},
timeWindows=新列表(){
新时间窗口{
startSec=43200,
端秒=54000
}},
notes=“通过API添加的订单”,
serviceTimeSec=1800,
tagsIn=新列表(),
tagsOut=新列表(),
customFields=新的customFields
{
myCustomField=“自定义字段内容”,
orderId=“abcd1234”
}
},
};
rootObj.orders.Add(订单);
返回rootObj;
}
创建一个通用的post方法。。(您需要添加HttpClient引用-NuGet)

TRSULT HttpPostRequest(字符串requestUrl,TREQ requestObject)
{
使用(var client=new HttpClient())
{
//您应该使用api密钥替换
client.DefaultRequestHeaders.Add(“X-WorkWave-Key”,“您的API密钥”);
client.BaseAddress=新Uri(“wrm.workwave.com”);
使用(var responseMessage=client.postasjssonasync(requestUrl,requestObject).Result)
{
TRSULT result=responseMessage.Content.ReadAsAsync().result;
返回结果;
}
}
}
现在,你可以提出请求了

var orderReqeuest = GetOrderRequestObject();
OrderResponse orderResponse = HttpPostRequest<OrderRequest, OrderResponse>("/api/v1/territories/429defc8-5b05-4c3e-920d-0bb911a61345/orders", orderReqeuest);
var orderequeest=getorderequestobject();
OrderResponse OrderResponse=HttpPostRequest(“/api/v1/Territions/429defc8-5b05-4c3e-920d-0bb911a61345/订单”,OrderRequeest);
更新:

对不起,我们需要添加对Microsoft ASP.NET Web API 2.2客户端库的引用


首先,您需要创建请求和响应对象。您可以使用c#类创建者,比如json2charp.com

public class Eligibility
{
    public string type { get; set; }
    public List<string> onDates { get; set; }
}

public class Loads
{
    public int people { get; set; }
}

public class Location
{
    public string address { get; set; }
}

public class TimeWindow
{
    public int startSec { get; set; }
    public int endSec { get; set; }
}

public class CustomFields
{

    public string myCustomField { get; set; }
    public string orderId { get; set; }
}

public class Delivery
{
    public Location location { get; set; }
    public List<TimeWindow> timeWindows { get; set; }
    public string notes { get; set; }
    public int serviceTimeSec { get; set; }
    public List<object> tagsIn { get; set; }
    public List<object> tagsOut { get; set; }
    //this field will be Dictionary...
    public CustomFields customFields { get; set; }
}

public class Order
{
    public string name { get; set; }
    public Eligibility eligibility { get; set; }
    public object forceVehicleId { get; set; }
    public int priority { get; set; }
    public Loads loads { get; set; }
    public Delivery delivery { get; set; }
}

public class OrderRequest
{
    public List<Order> orders { get; set; }
}
public class OrderResponse
{
    public string requestId { get; set; }
}
公共类资格
{
公共字符串类型{get;set;}
公共列表日期{get;set;}
}
公共类负载
{
公共整数人{get;set;}
}
公共类位置
{
公共字符串地址{get;set;}
}
公共类时间窗口
{
public int startSec{get;set;}
公共int-endSec{get;set;}
}
公共类自定义字段
{
公共字符串myCustomField{get;set;}
公共字符串orderId{get;set;}
}
公共课堂教学
{
公共位置位置{get;set;}
公共列表时间窗口{get;set;}
公共字符串注释{get;set;}
公共int-serviceTimeSec{get;set;}
公共列表标记{get;set;}
公共列表标记sout{get;set;}
//此字段将是字典。。。
公共自定义字段自定义字段{get;set;}
}
公共阶级秩序
{
公共字符串名称{get;set;}
公共资格{get;set;}
公共对象forceVehicleId{get;set;}
公共int优先级{get;set;}
公共负荷