在使用byte []调用服务时,它会给出400个错误请求

时间:2017-01-16 10:59:36

标签: c# json web-services serialization

我正在尝试使用byte []调用服务,但我得到了

  

400:请求不好。


我使用下面的代码。

=============================

班级结构

class DLDShipmentDeliveryStatusResponse
{
    public bool IsSuccess { get; set; }
    public ValidationErrors ValidationErrors { get; set; }

}
class ValidationErrors
{
    public int ErrorNumber { get; set; }
    public string ErrorMessage { get; set; }
    public string ErrorMessageAr { get; set; }
    public string ExceptionObject { get; set; }
    public int ValidationType { get; set; }
}
[Serializable]
class DLDServiceRequest
{
    public string AWB { get; set; }
    public int DeliveryStatus  { get; set; }
    public DateTime LastUpdateTime  { get; set; }
    public List<DLDShipmentDeliveryDocument> ProofOfDeliveryDocumentList { get; set; }
}
[Serializable]
class DLDShipmentDeliveryDocument
{
    public byte[] DocumentStream  { get; set; }
    public string DocumentExtension  { get; set; }
}

=======

致电服务

var shipmentDeliveryInfo = new DLDServiceRequest
{
    AWB = "1233455",
    DeliveryStatus = 1,
    LastUpdateTime = DateTime.Now
};

var deliveryDoc = new DLDShipmentDeliveryDocument
{
    DocumentStream = item.ProofOfDelivery,//Here assign value of byte[]
    DocumentExtension = "png"
};
shipmentDeliveryInfo.ProofOfDeliveryDocumentList = new List<DLDShipmentDeliveryDocument>();
shipmentDeliveryInfo.ProofOfDeliveryDocumentList.Add(deliveryDoc);

HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http:....");

httpWReq.Method = Constants.POST;
httpWReq.KeepAlive = false;
httpWReq.ContentType = Constants.JSONContentType;
httpWReq.Headers.Add("X-ApiKey", "-----");
httpWReq.AllowAutoRedirect = false;                                               

StreamWriter writer = new StreamWriter(httpWReq.GetRequestStream());
var shipmentJSON = JsonConvert.SerializeObject(shipmentDeliveryInfo);

writer.Write(shipmentJSON);
writer.Close();

json = String.Empty;
using (var response = (System.Net.HttpWebResponse)httpWReq.GetResponse())
{
    using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
        json = sr.ReadToEnd();
}
var DLDServiceDeptObj = JsonConvert.DeserializeObject<DLDShipmentDeliveryStatusResponse>(json);

这里我没有提到主机和API密钥值。

0 个答案:

没有答案