Trouble with HttpClient with form submission, encounter

problem

Small and micro businesses in the development of micro-channel pay into pieces when the interface is required to upload pictures and other identification data through forms. In the micro-channel interface document also shows that the payment is required multipart / form-data send requests. .NET provides the MultipartFormDataContenttype, helping us build a form request, and therefore have the following code:

var form = new MultipartFormDataContent()
{
    {new StringContent("Value"),"Name},
    {new ByteArrayContent(new byte[]{}/*模拟文件数据*/),"File,"FileName}
}

After submitting official documents in accordance with the micro-channel pay, has prompted the parameter error, baffled.

the reason

By Postman analog form submission, capturing packets, and compare it to the C # code is submitted, two found a problem.

Postman original submission:

POST http://api.mch.weixin.qq.com/secapi/mch/uploadmedia HTTP/1.1
User-Agent: PostmanRuntime/7.21.0
Accept: */*
Cache-Control: no-cache
Postman-Token: b6800c0f-3f16-4981-b661-e6d16fc1bb1e
Host: api.mch.weixin.qq.com
Content-Type: multipart/form-data; boundary=--------------------------639275760242036520206377
Accept-Encoding: gzip, deflate
Content-Length: 566
Connection: keep-alive

----------------------------639275760242036520206377
Content-Disposition: form-data; name="mch_id"

1565111111
----------------------------639275760242036520206377
Content-Disposition: form-data; name="media_hash"

7215E92A8F3F3D0256484EFFF53A25F6
----------------------------639275760242036520206377
Content-Disposition: form-data; name="sign_type"

HMAC-SHA256
----------------------------639275760242036520206377
Content-Disposition: form-data; name="sign"

A1D8B094FA24BE5531D1AC198DE25550
----------------------------639275760242036520206377--

C # code to submit:

POST http://api.mch.weixin.qq.com/secapi/mch/uploadmedia HTTP/1.1
Host: api.mch.weixin.qq.com
Content-Type: multipart/form-data; boundary="e9d5712f-7923-4ec5-8bf3-c8d5d3cd3217"
Content-Length: 502

--e9d5712f-7923-4ec5-8bf3-c8d5d3cd3217
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=mch_id


--e9d5712f-7923-4ec5-8bf3-c8d5d3cd3217
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=media_hash

33F15BC2D17D6FFBC18FA566EF65722E
--e9d5712f-7923-4ec5-8bf3-c8d5d3cd3217
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=sign

1E377684F9BD583D2ED26FB367916C0C
--e9d5712f-7923-4ec5-8bf3-c8d5d3cd3217--

1. Boundary double quotes

Use MultipartFormDataContentform submission request external Content-Typeto boundaryvalues with "numbers. Postman the request submitted form, its boudaryvalue is not double quotes.

Why would cause such a difference? Reference this blog to explain, it is because each system / language for realization of RFC 2046 caused by the inconsistency. For in MultipartFormDataContentbehavior, if the value of Boundary back with double quotation marks is standard.

RFC 2612 Original:

2) Although RFC 2046 [40] permits the boundary string to be
quoted, some existing implementations handle a quoted boundary
string incorrectly.

Boundary effect, a random string is generated, the HTTP protocol is divided among a plurality of internal Content. Why is randomly generated it? This is to prevent delimiter generate internal repeat with your Content caused the accident. (C # is used by default as Guid random string, you may be in the configuration MultipartFormDataContenttime, by manually specifying its constructor)

2. Form the key-value pair, double quotes value

The second problem is the content in the form of their namekey-value pairs, whose values and no double quotes, so you have to add Content of the time, have to manually specify the double quotation marks.

solve

Two problems are caused because the double quotation marks, it is only necessary before actually initiating calls to replace double quotes inside is empty, or double quotation marks to add the missing.

For a problem, its internal ContentType.Parameters, through LINQ to find the boundarykey-value pairs, you can replace the inside of the double quotes.

var boundaryValue = form.Headers.ContentType.Parameters.Single(p => p.Name == "boundary");
boundaryValue.Value = boundaryValue.Value.Replace("\"", String.Empty);

For question two, when the internal structure of the Content, given its Name to manually double quotes.

var form = new MultipartFormDataContent
{
    {new StringContent(mchId), "\"mch_id\""},
    {new ByteArrayContent(bytes), "media", $"\"{HttpUtility.UrlEncode(Path.GetFileName(imagePath))}\""},
    {new StringContent(mediaHash), "\"media_hash\""},
    {new StringContent(sign), "sign"}
};

Guess you like

Origin www.cnblogs.com/myzony/p/12114507.html