2012-04-19 3 views
1

나는 멀티 파트 마임 즉, 첨부 파일이있는 비누를 보내는 스크립트가 있습니다. C# httpWebRequest 클래스를 사용하고 있습니다. 내용 길이가 필요하다는 오류가 발생하지만 webrequest의 contentLength 속성을 사용하여 코드에서 내용 길이를 동적으로 설정하고 있습니다. 왜 이것이 될 수있는 아이디어? 고맙습니다!HTTP post contentlength error

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

+0

안녕하세요. 당신의 응답을위한 고맙습니다 .. 예 –

+0

@imonc ..i 업데이트 된 mmy 질문 .thanx –

답변

1

당신은 SendChunked에서 = 모두에 해당하고 ContentLength 사용할 수 없습니다 필요한 내용 길이는 다음과 같습니다 이 코드입니다. 가장 쉬운 방법은 청크 응답을 사용하고 ContentLength를 생략하는 것입니다. ContentLength를 유지하면서 원하는 경우 SendChunked를 생략 할 수도 있습니다.

익숙하지 않은 경우 위키 피 디아는 멋진 description of chunking입니다.

0

contentLength를 보내는 바이트 ('bytes')의 길이로 선언 한 다음 xmlDoc 길이를 콘텐츠 길이로 사용하면 실제 길이와 다른 길이가됩니다. 그런 다음 outputStream.Write에서 동일한 xmlDoc.Length를 사용합니다.

두 경우 모두 bytes.Length를 사용하지 않는 이유는 무엇입니까? 또는 당신이 선언했지만 결코 사용하지 않은 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(); 
} 

}