我正在使用HttpClient
进行多部分请求,并成功从服务器获得了多部分响应,一切看起来都很好。
RAW HTTP请求是(并且所有HTTP标头均已正确设置)
--batchquery
Content-Type: application/http
Content-Transfer-Encoding: binary
GET https://myinstance.dynamics.com/api/data/v9.1/adx_externalidentities?fetchXml=<fetch><entity name="adx_externalidentity"><all-attributes /><filter type="and"><condition attribute="adx_username" operator="eq" value="d2d5b3cc-133b-e911-a995-000d3ad1c6f2" /></filter><link-entity name="contact" from="contactid" to="adx_contactid" link-type="outer" alias="__0__"><all-attributes /></link-entity></entity></fetch> HTTP/1.1
--batchquery--
削减的http响应标头
Cache-Control: no-cache
Server: microsoft-IIS/10.0
strict-transport-security: max-age=31536000; includeSubDomains
OData-Version: 4.0
Public: OPTIONS,GET,HEAD,POST
Timing-Allow-Origin: *
Date: Fri,22 Nov 2019 06:01:11 GMT
Allow: OPTIONS
Allow: GET
Allow: HEAD
Allow: POST
Content-Type: multipart/mixed; boundary=batchresponse_c6fd33a6-4abe-4b17-87ec-44daf977e518
Expires: -1
Content-Length: <manually edited>
http响应有效载荷
--batchresponse_c6fd33a6-4abe-4b17-87ec-44daf977e518
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; charset=utf-8
OData-Version: 4.0
{"@odata.context":"https://myinstance.dynamics.com/api/data/v9.1/$metadata#adx_externalidentities(_adx_contactid_value,adx_contactid,_modifiedby_value,_organizationid_value,organizationid,adx_externalidentityid,statecode,adx_username,_createdby_value,createdon,statuscode,adx_identityprovidername,modifiedon,adx_contactid(),organizationid())","value":[{"@odata.etag":"W/\"22669488\"","_adx_contactid_value":"123"}
--batchresponse_c6fd33a6-4abe-4b17-87ec-44daf977e518--
在为内部Section创建自己的解析器之前,我希望可以在Framework中找到一个现有方法(例如StringContent),该方法可以使我返回HttpResponseMessage
对象或对该部分的内容,就好像它是标准的httpresponsemessage
private async Task<ResponseData> GetRequest(string requestUrl)
{
Authorise();
var mp = new MultipartContent("mixed","batchquery");
var sb = new StringBuilder();
sb.AppendLine($"GET {requestUrl} HTTP/1.1");
var content = new StringContent(sb.ToString());
content.Headers.ContentType = new MediaTypeHeaderValue("application/http");
content.Headers.Add("Content-Transfer-Encoding","binary");
mp.Add(content);
using (var batchRequest = new HttpRequestMessage
{
Method = HttpMethod.Post,Requesturi = BatchRequesturi,Content = mp,})
{
batchRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer",accessToken);
using (var response = await httpClient.SendAsync(batchRequest))
{
var mt = response.Content.Headers.ContentType;
var boundary = mt.Parameters.FirstOrDefault(x => x.Name == "boundary");
if (boundary == null) throw new InvalidDataException("Boundary not found in headers");
var mpr = new MultipartReader(boundary.Value,await response.Content.ReadAsStreamAsync());
while (true)
{
var section = await mpr.ReadNextSectionAsync();
if (section == null) break;
var responsedata = new ResponseData
{
Data = await section.ReadAsStringAsync(),StatusCode = response.StatusCode
};
return responsedata;
}
}
}
return null;
}
当我查看我的responseData.Data对象时,看到的是200 OK响应和所有标头,而不仅仅是Json Response数据,就像您直接将HttpClient与单个{{1} }
response.data包含...
GET