2011-09-13 12 views
1

我需要用xml數據發出POST請求。C#HttpWebRequest/Response給出了400個錯誤的請求。但不是Firefox的HTTP資源測試

String xml = ""; 
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml); 
HttpClient.post(url, data, "text/xml") 

然後我打電話POST功能:

public static String post(String url, byte[] data, String contentType){ 
     String body = ""; 
     body = getResponse("POST", url, data, contentType, 80); 
     return body; 
    } 

現在我調用這個函數來發出請求/得到響應:

public static String getResponse(String method, String url, byte[] data, String contentType, int serverPort) 
    { 
     String result = null; 
     HttpWebRequest request = sendRequest(method, url, data, contentType, serverPort); 
     HttpWebResponse response = null; 
     try 
     { 
      response = (HttpWebResponse)request.GetResponse(); 
      if (response != null){ 
       // Get the stream associated with the response 
       Stream receiveStream = response.GetResponseStream(); 
       // Pipes the stream to a higher level stream reader 
       StreamReader readStream = new StreamReader (receiveStream, System.Text.Encoding.UTF8); 
       result = readStream.ReadToEnd(); 
      } 
     } 
     catch(WebException ex) 
     { 
      if (ex.Status == WebExceptionStatus.ProtocolError){ 
       throw new HttpClientException("HTTP response error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription); 
      } 
      else{ 
       throw new HttpClientException("HTTP response error with status: " + ex.Status.ToString()); 
      } 
     } 
} 

public static HttpWebRequest sendRequest(String method, String url, byte[] data, String contentType, int serverPort){ 
     HttpWebRequest request = null; 
     try 
     { 
      UriBuilder requestUri = new UriBuilder(url); 
      requestUri.Port = serverPort; 
      request = (HttpWebRequest)WebRequest.Create(requestUri.Uri); 
      request.Method = method; 
      // 
      if ((method == "POST") && (data != null) && (data.Length > 0)){ 
       request.ContentLength = data.Length; 
       request.ContentType = ((String.IsNullOrEmpty(contentType))?"application/x-www-form-urlencoded":contentType); 
       Stream dataStream = request.GetRequestStream(); 
       dataStream.Write (data, 0, data.Length); 
       // Close the Stream object. 
       dataStream.Close(); 
      } 
     } 
     catch(WebException ex) 
     { 
      if (ex.Status == WebExceptionStatus.ProtocolError){ 
       throw new HttpClientException("HTTP request error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription); 
      } 
      else{ 
       throw new HttpClientException("HTTP request error with status: " + ex.Status.ToString()); 
      } 
     } 
} 

它總是給我一個HttpCliendException

video_api.HttpClientException: HttpClient exception :HTTP response error. with `code: 400` and `status: Bad Request` 

但是,當我使用Firefox插件HTTP Resource Test試了一下,它運行得很好,並得到202 Accepted status用相同的XML文檔。

在調用post請求之前,我安慰了內容類型和data.length,內容類型是text/xml,data.length143

+1

您可能需要一個'User-Agent'。 – SLaks

回答

1

我已經知道一些網站是挑剔的請求標題,並返回不同的結果,完全基於這些值。比較FireFox中HTTP請求的請求頭和你的請求,如果你在FireFox資源測試中模仿頭文件,它很可能會工作(Request.AddHeader("name", "value"))。另一個值得注意的區別可能是用戶代理對Web服務器來說可能是挑剔的。

2

使用fiddler(http://www.fiddler2.com/fiddler2/)查看Firefox正確發送了哪些標頭。然後看看你發送的標題有什麼不同。

0

在我的項目我在config.area

<defaultProxy useDefaultCredentials="true"> 
... 
    </defaultProxy> 

使用一些自定義的設置,它幫助我禁用代理:

request.Proxy = null; 
0

另一種解決方案將是一個搞砸了SSL和非SSL端口。

當您將純樸的http傳遞給https端口時,在Apache上回答400錯誤請求。

相關問題