带有内容类型的HttpClient设置边界 [英] HttpClient setting boundary with content-type

查看:168
本文介绍了带有内容类型的HttpClient设置边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript与第三方服务进行通信。作为身份验证过程的一部分,他们需要帖子消息的 multipart / form正文,包括要在md5中加密的图像,将其添加到包含日期和其他一些内容的字符串中,然后运行HMAc / SHA1在上面。因此最后,它们具有多部分主体,日期和身份验证哈希,以便进行身份验证,然后读取图像。

I am using javascript to communicate with a third party service. As part of their authentication process they need the "multipart/form" body of the post message including an image to be encrypted in md5, this is added to a string including the date, and a few other things and then a HMAc/SHA1 run on it. So in the end they have the multipart body, the date and the authentication hash in order to authenticate and then read the image.

这对所有移动设备都适用,除了windowsPhone ..(我知道,IE的问题...谁会想到的?)。他们的httpwebrequest不包含 Date标头..因此不进行身份验证。这意味着我必须使用Windows Phone并使用C#中新发布的httpclient代码。现在我是C#菜鸟,所以这可能是简单的解决方案所在。通过将几乎所有内容都传递给c#并仅使用c#进行发布,我已经通过了身份验证,但是它们无法读取正文,因为我发现发送边界的唯一方法是将内容定义为multipartformDatacontent并将内容发送给

This works fine for all mobile devices except for windowsPhone .. (I know, a problem with IE... who would have thought?). their httpwebrequest does not include a 'Date' header .. so no authentication. This means that I have to go native for windows phone and use the newly released httpclient code in C#. Now I am a C# noob so this is possibly where the easy solution is. I have gotten the authentication to work by passing pretty much everything to c# and just doing the post using c# but they cannot read the body because the only way I have found to send the boundary is when defining the content as multipartformDatacontent and sending the content that way changes the body so the authentication failes.

我的JavaScript类似于:

my javascript is something like:

var boundary = "------------ThIs_Is_tHe_bouNdaRY_";
var part1Array = [];
var part1 = "--"+boundary + "\r\n"+
    "Content-Disposition: form-data; name=\"image\"\r\n"+
    "Content-Type: image/jpg\r\n"+
    "\r\n";
var part3Array = [];
var part3 = "\r\n" + boundary +"--";
for(var p1=0; p1<part1.length; p1++){
    part1Array.push(part1.charCodeAt(p1));
}
for(var p3=0; p3<part3.length; p3++){
    part3Array.push(part3.charCodeAt(p3));
}
var bodyContent = part1Array.concat(imageArray,part3Array);

//hash this

var authMessage = bodyContentHash +"\n"+ contentType +"\n"+ todayString +"\n"+ pathandstuff;
// -hmac -sha1 -base64

,c#为:

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, path);

request.Headers.Date = DateTime.ParseExact(todaydate, "ddd',' dd MMM yyyy HH:mm:ss 'GMT'", new CultureInfo("en-US"), DateTimeStyles.AssumeUniversal);
request.Headers.Add("Accept", "application/json; charset=utf-8");
request.Headers.Add("Authorization", auth);

byte[] body = Convert.FromBase64String(bodyData);
request.Content = new ByteArrayContent(body);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
request.Content.Headers.Add("boundary", "------------ThIs_Is_tHe_bouNdaRY_");

HttpResponseMessage response = client.SendAsync(request).Result;
string resultCode = response.StatusCode.ToString();
string responseBodyAsText = await response.Content.ReadAsStringAsync();

这很有效..正文内容正确,标头也正确..类型标头,应为:

This pretty much works.. the body content is correct as are the headers .. all except the content type header which should be:

request.Content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data, boundary=------------ThIs_Is_tHe_bouNdaRY_");

除了这会引发System.FormatException错误。

Except that this throws a System.FormatException error.

推荐答案

 request.Content.Headers.Add("ContentType", "multipart/form-data, boundary=------------ThIs_Is_tHe_bouNdaRY_");

您可以使用HttpWebRequest

you can use HttpWebRequest

 myHttpWebRequest.Date = DateTime.Now;
 myHttpWebRequest.ContentType = "multipart/form-data, boundary=------------ThIs_Is_tHe_bouNdaRY_";

这篇关于带有内容类型的HttpClient设置边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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