HTTP发布内容长度错误 [英] HTTP post contentlength error

查看:73
本文介绍了HTTP发布内容长度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本发送多部分的哑剧,即带有附件的肥皂.我正在使用C#httpWebRequest类.我收到一条错误消息,指出内容长度是必需的,但是我正在使用webrequest的contentLength属性在代码中动态设置内容长度.任何想法为什么会这样?谢谢! 这是代码:

I have a script sending a multi part mime ie a a soap with attachments. I am using C# httpWebRequest class. I am getting an error that says the content length is required yet I am setting the content length dynamically in my code using webrequest's contentLength property. any ideas why this could be? thanx! this is the code:

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)

The error/exception i get is COntent length required (411)

推荐答案

您不能同时使用SendChunked = true和ContentLength.最简单的方法是使用分块响应并省略ContentLength.如果需要,您还可以保留ContentLength并省略SendChunked.

You can't use both SendChunked=true and ContentLength. The easiest thing to do would be to use a chunked response and omit the ContentLength. You could also retain the ContentLength and omit the SendChunked if you want though.

如果您不熟悉它,Wikipedia会提供漂亮的分块描述.

In case you're not familiar with it, wikipedia has a nice description of chunking.

这篇关于HTTP发布内容长度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆