相关疑难解决方法(0)

从HttpResponseMessage获取内容/消息

我正在尝试获取HttpResponseMessage的内容.它应该是:{"message":"Action '' does not exist!","success":false}但我不知道如何从HttpResponseMessage中获取它.

HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("http://****?action=");
txtBlock.Text = Convert.ToString(response); //wrong!
Run Code Online (Sandbox Code Playgroud)

在这种情况下,txtBlock将具有以下值:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Vary: Accept-Encoding
  Keep-Alive: timeout=15, max=100
  Connection: Keep-Alive
  Date: Wed, 10 Apr 2013 20:46:37 GMT
  Server: Apache/2.2.16
  Server: (Debian)
  X-Powered-By: PHP/5.3.3-7+squeeze14
  Content-Length: 55
  Content-Type: text/html
}
Run Code Online (Sandbox Code Playgroud)

c# windows-8 windows-store-apps

155
推荐指数
6
解决办法
20万
查看次数

如何从C#中的Web Api方法正确获取字节数组?

我有以下控制器方法:

[HttpPost]
[Route("SomeRoute")]
public byte[] MyMethod([FromBody] string ID)
{
  byte[] mybytearray = db.getmybytearray(ID);//working fine,returning proper result.
  return mybytearray;
}
Run Code Online (Sandbox Code Playgroud)

现在在调用方法(这也是另一个WebApi方法!)我写的像这样:

private HttpClient client = new HttpClient ();
private HttpResponseMessage response = new HttpResponseMessage ();
byte[] mybytearray = null;
response = client.GetAsync(string.Format("api/ABC/MyMethod/{0}", ID)).Result;
if (response.IsSuccessStatusCode)
{
    mybytearray = response.Content.ReadAsByteArrayAsync().Result;//Here is the problem
} 
Run Code Online (Sandbox Code Playgroud)

现在,问题是字节数​​组MyMethod发送的是528字节,但是在这之后ReadAsByteArrayAsync,大小变得更大(706字节)并且这些值也被放大了.

有点打击我的头,任何帮助将不胜感激.

谢谢!

c# asp.net-web-api

34
推荐指数
5
解决办法
7万
查看次数

将HttpContent转换为byte []

我目前正在研究ac #web API.对于特定的调用,我需要使用对API的ajax调用发送2个图像,以便API可以将它们保存为数据库中的varbinary(max).

  1. 如何从对象中提取Imagebyte[]HttpContent对象中提取?
  2. 我该怎么做两次?一次为每个图像.

-

var authToken = $("#AuthToken").val();
var formData = new FormData($('form')[0]);
debugger;
$.ajax({
    url: "/api/obj/Create/", 
    headers: { "Authorization-Token": authToken },
    type: 'POST',
    xhr: function () { 
        var myXhr = $.ajaxSettings.xhr();
        return myXhr;
    },
    data: formData,
    cache: false,
    contentType: false,
    processData: false
});
Run Code Online (Sandbox Code Playgroud)

-

public async Task<int> Create(HttpContent content)
{
    if (!content.IsMimeMultipartContent())
    {
        throw new UnsupportedMediaTypeException("MIME Multipart Content is not supported");
    }

    return 3;
}
Run Code Online (Sandbox Code Playgroud)

c# bytearray stream type-conversion httpcontext

8
推荐指数
2
解决办法
1万
查看次数