Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多部分/表单数据作为web请求时出现问题_C#_Xml_Multipartform Data_Webrequest - Fatal编程技术网

C# 多部分/表单数据作为web请求时出现问题

C# 多部分/表单数据作为web请求时出现问题,c#,xml,multipartform-data,webrequest,C#,Xml,Multipartform Data,Webrequest,我有一个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">Sen

我有一个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>

XXXXXX
XXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
股票
3109750
7.
这部分很好用

现在我试着用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();
WebRequest req=WebRequest.Create(“http://RemoteServerURL");
字符串xmlmsg=“XXXXXXXXXXXXX-XXXX-XXXXX-XXXXX-XXXXXXXXX STOCK31097507”;
byte[]send=Encoding.Default.GetBytes(xmlmsg);
请求方法=“POST”;
req.ContentType=“多部分/表单数据”;
req.ContentLength=发送.Length;
Stream sout=req.GetRequestStream();
sout.Write(send,0,send.Length);
sout.Flush();
sout.Close();
WebResponse res=req.GetResponse();
StreamReader sr=新的StreamReader(res.GetResponseStream());
字符串returnvalue=sr.ReadToEnd();
这失败了,我明白了

“由于找不到多部分边界,请求被拒绝 请求“


。因此,我知道对服务器的请求正在工作。但是输入或某些东西出了问题。请帮助摆脱
sout.Flush(),这不是必需的。

错误消息似乎很清楚,它要求多部分边界。 内容类型:多部分/混合;boundary=“boundary”


我知道这是一个旧线程,但我想与社区分享我的解决方案,因为在过去的一周中,我在执行多部分请求时遇到了类似的问题。我不能使用RestSharp,因为它不支持表单数据以外的内容类型,并且我需要“相关的”。我还需要基于带有json元数据的pdf的base64字符串。下面是我使用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;
    }

是的,信息很清楚。但是我应该在代码中做什么更改来纠正这个错误呢;将此更改为req.ContentType=“多部分/混合;边界=\”边界“”