将MultiPart/Form数据作为web请求时出现问题
本文关键字:请求 问题 web MultiPart Form 数据 | 更新日期: 2023-09-27 18:20:35
我有一个HTML POST请求,需要在c#中复制
HTML有点像
<FORM action="http://RemoteServerURL" enctype="multipart/form-data" method=POST>
<TEXTAREA id="TextAreaXML" name="xmlmsg" rows="20" cols="100"> </TEXTAREA>
<button type="submit">Send</button>
</form>
TextArea需要一个inout,如下所示
<?xml version="1.0" encoding="utf-8"?>
<OnlineCheck>
<Header>
<BuyerAccountId>XXXXXX</BuyerAccountId>
<AuthCode>XXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</AuthCode>
<Type>STOCK</Type>
</Header>
<Item line="1">
<ManufacturerItemIdentifier />
<DistributorItemIdentifier>3109750</DistributorItemIdentifier>
<Quantity>7</Quantity>
</Item>
</OnlineCheck>
这部分很好用。
现在我试着在c#中复制它,如下所示。
WebRequest req = WebRequest.Create("http://RemoteServerURL");
string xmlmsg = "<?xml version=" + '"' + "1.0" + '"' + " encoding==" + '"' + "utf-8" + '"' + "?><OnlineCheck><Header><BuyerAccountId>XXXXXX</BuyerAccountId><AuthCode>XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX</AuthCode><Type>STOCK</Type></Header><Item line=" + '"' + "1" + '"' + "><ManufacturerItemIdentifier /><DistributorItemIdentifier>3109750</DistributorItemIdentifier><Quantity>7</Quantity></Item></OnlineCheck>";
byte[] send = Encoding.Default.GetBytes(xmlmsg);
req.Method = "POST";
req.ContentType = "multipart/form-data";
req.ContentLength = send.Length;
Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
这失败了,我收到消息
"请求被拒绝,因为找不到任何多部分边界XML请求"
所以我知道对服务器的请求正在工作。但输入或出现问题。请帮助
去掉sout.Flush();
,没有必要。
错误消息似乎很清楚,它要求多部分边界。内容类型:多部分/混合;boundary="boundary"
HTTP多部分(POST)请求中的边界参数是什么?
我知道这是一个旧线程,但我想与社区分享我的解决方案,因为在过去的一周里,我在处理多部分请求时遇到了类似的问题。我不能使用RestSharp,因为它不支持表单数据以外的内容类型,我需要"相关"。我还需要在pdf的base64字符串中添加json元数据。下面是我使用HttpClient和HttpRequestMessage编写的一个示例函数。我认为你可以采用这种结构,很容易地将标题换成你想要做的事情
public static void PostBase64PdfHttpClient(string recordID, string docName, string pdfB64)
{
string url = $"baseURL";
HttpClient client = new HttpClient();
var myBoundary = "------------ThIs_Is_tHe_bouNdaRY_";
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes($"UN:PW"));
client.DefaultRequestHeaders.Add("Authorization", $"Basic {auth}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"{url}/api-endpoint");
request.Headers.Date = DateTime.UtcNow;
request.Headers.Add("Accept", "application/json; charset=utf-8");
MultipartContent mpContent = new MultipartContent("related", myBoundary);
mpContent.Headers.TryAddWithoutValidation("Content-Type", $"multipart/related; boundary={myBoundary}");
dynamic jObj = new Newtonsoft.Json.Linq.JObject(); jObj.ID = recordID; jObj.Name = docName;
var jsonSerializeSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
var json = JsonConvert.SerializeObject(jObj, jsonSerializeSettings);
mpContent.Add(new StringContent(json, Encoding.UTF8, "application/json"));
mpContent.Add(new StringContent(pdfB64, Encoding.UTF8, "application/pdf"));
request.Content = mpContent;
HttpResponseMessage response = client.SendAsync(request).Result;
}