HTT prequest和POSTHTT、prequest、POST

2023-09-03 01:10:07 作者:醉和困倦

我不断收到以下错误消息之一:

 远程服务器返回错误:(400)错误的请求
               要么
System.Net.ProtocolViolationException:您必须调用[开始]之前的GetResponse写CONTENTLENGTH字节请求流。
 

下面是code我快:

  StringBuilder的BLD =新的StringBuilder();
        bld.Append(contractId =);
        bld.Append(ctrId);
        bld.Append(与& companyIds =);
        bld.Append('+公司1 +,+ Company2的+');

        HttpWebRequest的REQ =(HttpWebRequest的)的WebRequest
            .Create(secureServiceUrl +SetContractCompanyLinks);
        req.Credentials = service.Credentials;
        //req.AllowWriteStreamBuffering = TRUE;
        req.Method =POST;
        req.Co​​ntentType =应用/的X WWW的形式urlen codeD;
        req.Co​​ntentLength = bld.Length;
        StreamWriter的作家=新的StreamWriter(req.GetRequestStream());
        变种EN codedData = Encoding.ASCII.GetBytes(bld.ToString());
        writer.Write(EN codedData);
        writer.Flush();
        writer.Close();
        VAR RESP = req.GetResponse();
 

解决方案

一对夫妇的事情是关:

直接写信给你的作家 不应该有一个理由骂的GetBytes()。该StreamWriter的是完全能够写一个字符串流的:

  writer.Write(bld.ToString());
 
在Python项目中使用 request post功能上传文件时需要注意哪些问题

使用using(){}模式在你的StreamWriter

这将确保妥善处置作家的对象。

 使用(VAR作家=新的StreamWriter(req.GetRequestStream()))
{
   writer.Write(bld.ToString());
}
 

您不必明确设置内容长度 息事宁人,根据你写的请求流的框架,将其设置为你。

如果您需要明确有关使用ASCII,在Content-Type头设置字符集

  req.Co​​ntentType =应用/的X WWW的形式urlen codeD;字符集= ASCII;
 

实例您的StreamWriter时,您还必须指定编码方式:

 新的StreamWriter(req.GetRequestStream(),Encoding.ASCII)
 

I keep getting one of the following error messages :

"The remote server returned an error: (400) Bad Request."  
               OR
"System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse."

Here is the code I'm running :

        StringBuilder bld = new StringBuilder();
        bld.Append("contractId=");
        bld.Append(ctrId);
        bld.Append("&companyIds=");
        bld.Append("'" + company1+ ", " + company2+ "'");

        HttpWebRequest req = (HttpWebRequest)WebRequest
            .Create(secureServiceUrl + "SetContractCompanyLinks");
        req.Credentials = service.Credentials;
        //req.AllowWriteStreamBuffering = true;
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = bld.Length;
        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        var encodedData = Encoding.ASCII.GetBytes(bld.ToString());
        writer.Write(encodedData);
        writer.Flush();
        writer.Close();
        var resp = req.GetResponse();

解决方案

A couple things that are "off":

Write directly to your writer There shouldn't be a reason to call GetBytes(). The StreamWriter is perfectly capable of writing a string to the stream:

writer.Write(bld.ToString());

Use the using() {} pattern around your StreamWriter

This will ensure proper disposal of the writer object.

using(var writer = new StreamWriter(req.GetRequestStream()))
{
   writer.Write(bld.ToString());
}

You don't need to explicitly set the content length Leave it alone, the framework will set it for you based on what you write to the request stream.

If you need to be explicit about using ASCII, set the charset in the Content-Type header

req.ContentType = "application/x-www-form-urlencoded; charset=ASCII";

You should also specify the encoding when instantiating your StreamWriter:

new StreamWriter(req.GetRequestStream(), Encoding.ASCII)