【发布时间】:2021-06-30 14:26:14
【问题描述】:
我正在尝试使用 Core.WebUtils.ApiCall 从 API 获取信息。基本上试图在 DHL 的 API 上创建一个货件。 (我有一个测试密钥) 对我来说最大的问题是错误消息显示没有任何帮助。发生错误的行旁边没有“消息”。我不知道会发生什么。
这是一段代码:
public static DHLShipmentResponse CreateAPIDHLShipment(DhlapiCall shipment, string apiKey)
{
string url = "https://api-gw.dhlparcel.nl/";
string urlParameters = "shipments";
var byteArray = Encoding.ASCII.GetBytes(apiKey + ":");
var auth = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var postcontent = JsonConvert.SerializeObject(shipment);
var test = ApiCall.RunAsync<DHLShipmentResponse>(url, urlParameters, auth, postcontent);
var response = ApiCall.RunAsync<DHLShipmentResponse>(url, urlParameters, auth, postcontent).GetAwaiter().GetResult();
return response;
}
相当小,相当基本,有一些测试看看我是否能找到任何东西,但无济于事。 只是为了更好地衡量,我使用“PostContent”的值通过邮递员运行它并且没有问题。所以我确定内容是正确的。我再次检查了网址,一切都很好。据我所知,我们一直在以这种方式使用身份验证(不记名令牌)。
这是我通过 Postman 得到的:
{
"shipmentId": "69028c6a-ce50-4f2e-9483-27fd0a8f72ec",
"product": "EPL-INT",
"pieces": [
{
"labelId": "69028c6a-ce50-4f2e-9483-27fd0a8f72ec",
"trackerCode": "SAMPLE0000180041",
"parcelType": "PALLET",
"pieceNumber": 1,
"weight": 0,
"labelType": "B2X_Generic_A4_Third"
}
],
"customsDeclarationId": "9a316738-3ef3-4471-9bd8-7c92deb50338"
}
这是响应的定义:
public partial class DHLShipmentResponse
{
[JsonProperty("shipmentId")]
public Guid ShipmentId { get; set; }
[JsonProperty("product")]
public string Product { get; set; }
[JsonProperty("shipmentTrackerCode")]
public string ShipmentTrackerCode { get; set; }
[JsonProperty("pieces")]
public List<PieceResponse> Pieces { get; set; }
[JsonProperty("returnShipment")]
public ReturnShipmentResponse ReturnShipment { get; set; }
[JsonProperty("customsDeclarationId")]
public string CustomsDeclarationId { get; set; }
}
public partial class PieceResponse
{
[JsonProperty("labelId")]
public Guid LabelId { get; set; }
[JsonProperty("trackerCode")]
public string TrackerCode { get; set; }
[JsonProperty("printlessCode")]
public string PrintlessCode { get; set; }
[JsonProperty("parcelType")]
public string ParcelType { get; set; }
[JsonProperty("pieceNumber")]
public long PieceNumber { get; set; }
[JsonProperty("weight")]
public long Weight { get; set; }
[JsonProperty("dimensions")]
[JsonConverter(typeof(ParseStringConverter))]
public long Dimensions { get; set; }
[JsonProperty("labelType")]
public string LabeType { get; set; }
}
我已经做了几个小时了,但无济于事。谢谢你的帮助。
【问题讨论】: