2012-04-19 12 views
1

私は、マルチパートのMIMEを送信するスクリプトを持っています。つまり、添付ファイル付きのSOAPです。私はC#のhttpWebRequestクラスを使用しています。コンテンツの長さが必要ですが、webrequestのcontentLengthプロパティを使用してコード内でコンテンツの長さを動的に設定しているというエラーが表示されます。なぜこれができたのでしょうか?ありがとう! これはコードです:HTTPポストコンテンツ長エラー

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

私が取得エラー/例外はあなたが真SendChunked =とん。ContentLengthの両方を使用することはできません(411)

+0

あなたが問題を示していますいくつかのコードを投稿し、例外メッセージをください含まことはできますか? – simonc

+0

こんにちは。あなたの応答のためのThanx .. ex –

+0

@imonc ..i更新されたmmy質問..thanx –

答えて

1

を必要なコンテンツの長さです。一番簡単なことは、チャンクされたレスポンスを使用し、ContentLengthを省略することです。また、ContentLengthを保持し、必要に応じてSendChunkedを省略することもできます。

あなたがそれに精通していない場合には、ウィキペディアは素敵なdescription of chunkingを持っています。

0

送信するバイト[]の長さとしてcontentLengthを宣言してから、コンテンツの長さとしてxmlDocの長さを使用します。実際の長さとは異なる長さになります。次に、同じxmlDoc.LengthをoutputStream.Writeで使用します。

どちらの場合も、バイト長を使用しないのはなぜですか?またはあなたが宣言したが使用したことのない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(); 
} 

}

関連する問題