HTTP post contentlength错误

时间:2012-04-19 10:09:10

标签: httpwebrequest

我有一个脚本发送多部分mime,即带有附件的肥皂。我正在使用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)

2 个答案:

答案 0 :(得分:1)

您不能同时使用SendChunked = true和ContentLength。最简单的方法是使用分块响应并省略ContentLength。您也可以保留ContentLength并省略SendChunked。

如果你不熟悉它,维基百科有一个很好的description of chunking

答案 1 :(得分:0)

您将contentLength声明为您发送的byte []的长度('bytes'),然后使用xmlDoc的长度作为内容长度 - 这将给出与实际不同的长度。然后在outputStream.Write中使用相同的xmlDoc.Length。

为什么你不使用bytes.Length?或者你已声明但从未使用过的contentLength,我相信这是你的问题,你发送的是xmlDoc.Length(字符串的长度)为bytes.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();
}

}