C# - ByteArray和字符串以及HTTPClient对象

时间:2015-11-19 07:22:43

标签: c# json asp.net-web-api httpclient

我试图点击第三方API(无法共享,保密),他们希望以下json作为输入:

{ 
PFID: “abc”,
CY: 2015,
AZs: [
    { AZN: “AZ1”, 
        x: <bytes>, y: <bytes>, x: <bytes>
    },
    { AZN: “AZ2”, 
        x: <bytes>, y: <bytes>, z: <bytes>
    }
]
}

我正在使用HTTPClient向此WebAPI提交请求。以下是该片段:

public void AddApplication(AZR request)
{
    HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(url);

             client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

        client.DefaultRequestHeaders.Add("authorization","mybearertoken");

    innerJson = Json.JsonParser.Serialize<AppZoneRequest>(request); // <=== coming perfectly till here. json returns a perfect json with in bytes flowing in a perfect hierarchy
    HttpContent content = new StringContent(innerJson, Encoding.UTF8, "application/json"); // <==== my best guess is problem is here
    HttpResponseMessage response = client.PostAsync(apiURL, content).Result;    
    return response;
}


  public class AZR
    {
        public string PFID { get; set; }
        public int CY { get; set; }
        public List<xyz> AZs { get; set; }
    }
    public class xyz
    {
        public string AZN { get; set; }
        public byte[] x { get; set; }
        public byte[] y { get; set; }
        public byte[] z { get; set; }
    }

当我发布请求时,它返回状态500,这表明内部服务器错误。我知道你不能在字符串内容中序列化字节数组,我们需要使用ByteArrayContent,如果你有两个,那么使用HttpMultipartFormDataContent。但是在这里,层次结构是这样的,我有string,int和一个对象数组,其中的对象包含string,byte [],byte [],byte []。现在,我真的无法找到提交帖子请求的方法。有什么帮助吗?

0 个答案:

没有答案