2012-04-19 102 views
1

我有一个脚本发送多部分MIME,即带有附件的肥皂。我正在使用C#httpWebRequest类。我收到一个错误,说内容长度是必需的,但我正在使用webrequest的contentLength属性在我的代码中动态地设置内容长度。任何想法,为什么这可能是?感谢名单! 这是代码:HTTP POST内容长度错误

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)

+0

你能发布一些代码,演示了问题,包括异常消息吗? – simonc 2012-04-19 11:03:51

+0

嗨。 Thanx为您的回应.. ex – 2012-04-19 12:18:36

+0

@simonc ..更新了mmy问题..thanx – 2012-04-19 12:30:35

回答

1

您不能同时使用SendChunked =真实的ContentLength内容长度。最简单的做法是使用分块响应并省略ContentLength。如果你愿意,你也可以保留ContentLength并省略SendChunked。

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

0

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

}