为什么尝试 POST 到 Rest API 会导致内部错误 500?

Why does this attempt to POST to Rest API result in Internal Error 500?

我能够从这个 API 成功获取订单,但无法 POST。我已经尝试了很多方法来 POST 到这个 API,但是一切都会导致内部服务器错误 500。有没有人从给出的 PHP 代码中看到我做错了什么,以及它是如何做的可以更改为在 C# 中正常工作吗?我使用与 API.

中的 GET 非常相似的代码

这是 C# 代码:

ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] postBytes = encoding.GetBytes(jsondata);
// used to build entire input
StringBuilder sb = new StringBuilder();

// used on each read operation
byte[] buf = new byte[8192];

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(geturl);
//request.Credentials = new NetworkCredential(supplierid, token);
request.Method = "POST";
request.Accept = "application/json";
request.UserAgent = "curl/7.37.0";
request.ContentLength = postBytes.Length;
request.ContentType = "application/json";
SetBasicAuthHeader(request, supplierid, token);
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Close();

try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream resStream = response.GetResponseStream();

string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);

// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
Console.WriteLine(tempString);

// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
}

catch (WebException ex)
{   
Console.WriteLine(ex.ToString());
logger.Log(ex.ToString());
//Console.ReadLine();
}

这里是PHP中给出的例子:

<?php
// Create a new order
$username = "foo@foodomain.com";
$password = "qaz@PASSword.net";
$base_url = "https://my.freestylecommerce.com/webapi/V1/";

$data_json ='{"OrderNumber":"123495","OrderDate":"01/07/2015 5:06:39 AM","ShippingMethod":"2-Day Domestic","BillingAddress":{"FirstName":"Joe","LastName":"Test","Company":"","AddressLine1":"9 Sevilla Rd","AddressLine2":"","City":"Andover","State":"MA","Postcode":"01810","Country":"US","Email":"joet@yahoo.com",

"Phone":"9785551234"},"ShippingAddress":{"FirstName":"Joe","LastName":"Test","Company":"","AddressLine1":"9 Sevilla Rd","AddressLine2":"","City":"Andover","State":"MA","Postcode":"01810","Country":"US","Email":"joet@yahoo.com",

"Phone":"9785551234"},"UpdateAt":"01/07/2015 5:06:39 AM","OrderStatus":12,"TotalAmount":22.70,"PaidAmount":0.0,"ShippingAmount":6.95,"TaxAmount":0.00,"OrderItems":
[{"ProductSKU":"PF7977","Quantity":"1.00","Price":"15.75","Discount":"0.00"}],"SalesChannelId":"f5defa6d-9566-4705-b201-a3e90170895a"}';

$url = $base_url . "Orders";
print("<br/><br/>***** POST to URL ". $url . "<br/>");
$out = post_request ($data_json,  $url, $username, $password);
print($out);

/*
* Curl POST request
*/

function post_request ($data_json, $url, $username, $password)
{

    $CURL = curl_init();

    // Set Curl Parameters
    curl_setopt($CURL, CURLOPT_URL, $url); // set url to post to
    curl_setopt($CURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($CURL, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json','Content-Length: ' . strlen($data_json)));

    curl_setopt($CURL, CURLOPT_POST, 1);
    curl_setopt($CURL, CURLOPT_POSTFIELDS,$data_json);
    curl_setopt($CURL, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($CURL, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($CURL, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($CURL, CURLOPT_SSL_VERIFYHOST, 0);

        // Curl Response
    $Response = curl_exec($CURL);
    if($Response === false)
    {
        print(curl_error($CURL));
    }
    curl_close($CURL);
    return $Response;
}
?>

根据 PHP 的网站,http://php.net/manual/en/function.curl-setopt.php

CURLOPT_POSTFIELDS: This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.

您可以使用 json_decode 获取数组或使用 http_build_query 获取 urlencoded 字符串