消费网页API作为HttpPost [英] Consuming Web Api's as HttpPost

查看:305
本文介绍了消费网页API作为HttpPost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何张贴long类型到的WebAPI的数组。

我想我必须使用PostAsync从HttpClient的,但我不知道如何把数组HttpContent。

这是我的控制器。

  [HttpPost]
 [授权]
 公共无效UpdateBatchesToReadyToShip(长[] batchIds)
 {
            //处理请求
  }

这就是我正在尝试消耗API

  VAR缓冲= Encoding.ASCII.GetBytes(用户名:密码);
VAR authHeader =新AuthenticationHeaderValue(基本,Convert.ToBase64String(缓冲));
client.DefaultRequestHeaders.Authorization = authHeader;
VAR ARR =新长[3];
改编[0] = 10;
改编[1] = 12;
ARR [2] = 13;
(在ARR选择i.ToString())的string.join(,,从i)HttpContent含量=新的StringContent;
VAR任务= client.PostAsync(\"https://<uri>/api/orderprocessing/UpdateBatchesToReadyToShip\",content:content);
如果(task.Result.Status code ==的HTTPStatus code.Unauthorized)
{
Console.WriteLine(错误资格证书);
}
其他
{
//task.Result.EnsureSuccessStatus$c$c();
HTT presponseMessage消息= task.Result;
如果(message.IsSuccessStatus code)
{
VAR细节= message.Content.ReadAsStringAsync()结果。
Console.Write(详细信息);
}
}

和我得到这个例外

  {状态code:500,ReasonPhrase:内部服务器错误,版本:1.1,内容:System.Net.Http.StreamContent,头:
{
  编译:无缓存
  缓存控制:无缓存
  日期:星期五,2012十二时37分44秒格林尼治标准​​时间09 11
  服务器:Microsoft-IIS / 8.0
  的X ASPNET-版本:4.0.30319
  的X已启动方式:ASP.NET
  内容长度:1060
  内容类型:应用程序/ JSON的;字符集= UTF-8
  过期:-1
}}


解决方案

而不是使用的StringContent的,你可以使用ObjectContent:

  VAR内容=新ObjectContent&LT;长[]&GT;(ARR,新JsonMediaTypeFormatter());VAR任务= client.PostAsync(
    https://开头&LT;&URI GT; / API / orderprocessing / UpdateBatchesToReadyToShip
    内容:内容);

Can somebody help me how to post an array of type long to a WebApi.

I think I have to use PostAsync from HttpClient, but I am not sure how to put array to HttpContent.

This is my controller.

 [HttpPost]
 [Authorize]
 public void UpdateBatchesToReadyToShip(long[] batchIds)
 {
            // process request
  }

And this is how I am trying to consume API

var buffer = Encoding.ASCII.GetBytes("username:password");
var authHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(buffer));
client.DefaultRequestHeaders.Authorization = authHeader;
var arr = new long[3];
arr[0] = 10;
arr[1] = 12;
arr[2] = 13;
HttpContent content = new StringContent(string.Join(",", from i in arr select i.ToString()));
var task = client.PostAsync("https://<uri>/api/orderprocessing/UpdateBatchesToReadyToShip",content:content);
if (task.Result.StatusCode == HttpStatusCode.Unauthorized)
{
Console.WriteLine("wrong credentials");
}
else
{
//task.Result.EnsureSuccessStatusCode();
HttpResponseMessage message = task.Result;
if (message.IsSuccessStatusCode)
{
var details = message.Content.ReadAsStringAsync().Result;
Console.Write(details);
}
}

And I am getting this exception

{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  Cache-Control: no-cache
  Date: Fri, 09 Nov 2012 12:37:44 GMT
  Server: Microsoft-IIS/8.0
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 1060
  Content-Type: application/json; charset=utf-8
  Expires: -1
}}

解决方案

Instead of using StringContent, you can use ObjectContent:

var content = new ObjectContent<long[]>(arr, new JsonMediaTypeFormatter());

var task = client.PostAsync(
    "https://<uri>/api/orderprocessing/UpdateBatchesToReadyToShip",
    content:content);

这篇关于消费网页API作为HttpPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆