1

我有一个发送多部分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)

4

2 回答 2

1

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

如果您不熟悉它,维基百科对分块有很好的描述

于 2012-04-19T13:20:20.560 回答
0

您将 contentLength 声明为您发送的 byte[] 的长度('bytes'),然后使用 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();
}

}

于 2013-02-01T19:19:52.000 回答