我正在尝试设置HttpRequestMessage
的标头,以便向SOAP服务发送multipart/related
HTTP消息。
标题需要看起来像这样:
多部分/相关;边界= MIMEBoundaryurn_uuid_7B970E2B89C4446286DDFDBFF7F19581;类型= “应用/ XOP + xml” 的;开始= “0.urn:UUID:4628599A1A934415B3EFB15A1888004B@ws.jboss.org>”;启动信息=“应用/肥皂+ xml”的;行动= “瓮:IHE:ITI:2007:ProvideAndRegisterDocumentSet-B”
但我无法弄清楚如何使用HttpRequestMessage
设置它们。
这是我的代码:
using (var client = new HttpClient(handler))
{
var request = new HttpRequestMessage()
{
RequestUri = new Uri(Host),
Method = HttpMethod.Post
};
var boundary = Guid.NewGuid().ToString();
var bytes = new ByteArrayContent(doc);
bytes.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
var multi = new MultipartContent("related", boundary) {
new StringContent(xml.ToString(), Encoding.UTF8, "text/xml"),
bytes
};
request.Content = multi;
Log.Information(request.Headers.ToString());
Log.Information(request.Content.Headers.ToString());
Log.Information(request.Content.ToString());
var response = client.SendAsync(request).Result;
if (!response.IsSuccessStatusCode)
throw new Exception($"Request resulted in status :{response.StatusCode}");
var stream = response.Content.ReadAsStreamAsync().Result;
using (var sr = new StreamReader(stream))
{
var soapResponse = XDocument.Load(sr);
Log.Information("Soap response : " + soapResponse);
}
}