HTTP post contentlength error

2019-05-31 04:51发布

问题:

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();
    }
}

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

回答1:

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.

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



回答2:

You declare contentLength as the length of the byte[] that you send ('bytes') and then you use the length of xmlDoc as the content length - which will give a different length than actual. Then you use the same xmlDoc.Length in outputStream.Write.

Why are you not using bytes.Length in both cases? Or contentLength which you've declared but never used, I believe that is your problem, you are sending xmlDoc.Length (Length of a string) for bytes.Length (length of a byte array converted from a string), so when the file is longer than expected by the sent length, the error returns.

Try this:

    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();
}

}