HTTP后CONTENTLENGTH错误(HTTP post contentlength error

2019-09-16 15:15发布

我有一个脚本发送多部分MIME即AA带附件的SOAP。 我使用C#HttpWebRequest类。 我正写着的内容长度是必需的但我使用的WebRequest的CONTENTLENGTH属性动态地设置内容长度在我的代码错误。 任何想法,这可能是为什么? 感谢名单! 这是代码:

public void sendSOAPoverHttpVoda()
{
    try
    {
        string xmlDoc = this.soapXml;
        byte[] bytes;
        bytes = Encoding.UTF8.GetBytes(xmlDoc);
        long contentSize = bytes.Length;

        HttpWebRequest req =  (HttpWebRequest)WebRequest.Create(conectionUrl);
        req.SendChunked = true;
        CredentialCache credentialCacheObj = new CredentialCache();
        credentialCacheObj.Add(
            new Uri(conectionUrl),
            "Basic", new NetworkCredential("dddfff", "dddddd"));
        credentialCacheObj.Add(new Uri(conectionUrl), "Digest", new NetworkCredential("dddfff", "dddddd"));

        req.Credentials = credentialCacheObj;
        req.Method = "POST";
        req.ProtocolVersion = HttpVersion.Version11;

        req.ContentLength = xmlDoc.Length;
        req.ContentType = "multipart/related; boundary=\"----=cellsmart_mm7_SOAP\";type=\"text/xml\";Start=\"</cellsmart_mm7_submit>\"";
        req.AllowWriteStreamBuffering = true;

        req.Timeout = 20000;
        req.Headers.Add("SOAPAction", "");
        //req.Connection = "";

        if (bytes != null)
        {
            using (Stream outputStream = req.GetRequestStream())
            {
                outputStream.Write(bytes, 0, xmlDoc.Length);
            }
        }
        using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
        {
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            string responseText = reader.ReadToEnd();
            Console.WriteLine("Response received from URI : " + responseText);
            Console.ReadLine();
        } 

        Console.ReadLine();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error : " + ex.Message + "\n" + ex.StackTrace);
        Console.ReadLine();
    }
}

误差/异常我得到的是所要求的内容的长度(411)

Answer 1:

你不能同时使用SendChunked =真实的ContentLength。 最容易做的事情是使用一个分块响应和省略的ContentLength。 你也可以保留的ContentLength,如果你想,虽然省略SendChunked。

如果你不熟悉它,维基百科有一个漂亮的分块的说明 。



Answer 2:

您声明CONTENTLENGTH作为字节[]您发送(“字节”)的长度,然后你用xmlDoc中的内容长度的长度 - 这将给予不同的长度大于实际。 然后你使用outputStream.Write相同xmlDoc.Length。

你为什么不使用bytes.Length在这两种情况下? 或者你已经声明,但从未使用过CONTENTLENGTH,我相信这是你的问题,您是bytes.Length(一个字节数组从字符串转换而来的长度)发送xmlDoc.Length(字符串长度),因此该文件时长于由发送长度,错误返回预期。

尝试这个:

    public void sendSOAPoverHttpVoda()
    {
    try
    {
    string xmlDoc = this.soapXml;
    byte[] bytes;
    bytes = Encoding.UTF8.GetBytes(xmlDoc);

    HttpWebRequest req =  (HttpWebRequest)WebRequest.Create(conectionUrl);
    req.SendChunked = true;
    CredentialCache credentialCacheObj = new CredentialCache();
    credentialCacheObj.Add(
        new Uri(conectionUrl),
        "Basic", new NetworkCredential("dddfff", "dddddd"));
    credentialCacheObj.Add(new Uri(conectionUrl), "Digest", new NetworkCredential("dddfff", "dddddd"));

    req.Credentials = credentialCacheObj;
    req.Method = "POST";
    req.ProtocolVersion = HttpVersion.Version11;

    req.ContentLength = bytes.Length;
    req.ContentType = "multipart/related; boundary=\"----=cellsmart_mm7_SOAP\";type=\"text/xml\";Start=\"</cellsmart_mm7_submit>\"";
    req.AllowWriteStreamBuffering = true;

    req.Timeout = 20000;
    req.Headers.Add("SOAPAction", "");
    //req.Connection = "";

    if (bytes != null)
    {
        using (Stream outputStream = req.GetRequestStream())
        {
            outputStream.Write(bytes, 0, bytes.Length);
        }
    }
    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
    {
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        string responseText = reader.ReadToEnd();
        Console.WriteLine("Response received from URI : " + responseText);
        Console.ReadLine();
    } 

    Console.ReadLine();
}
catch (Exception ex)
{
    Console.WriteLine("Error : " + ex.Message + "\n" + ex.StackTrace);
    Console.ReadLine();
}

}



文章来源: HTTP post contentlength error