2013-01-03 33 views
0

我使用簡單的.NET服務(asmx),通過測試表單(POST)調用時工作正常。當通過HttpWebRequest對象調用時,我得到一個WebException「System.Net.WebException:遠程服務器返回錯誤:(500)內部服務器錯誤。」深入挖掘,閱讀WebException.Response.GetResponseStream()我得到消息:「Missing parameter:serviceType。」但我清楚地包含了這個參數。.NET服務響應500內部錯誤和「缺少參數」到HttpWebRequest POSTS,但測試表單工作正常

我在這裏很茫然,更糟糕的是我沒有權限調試服務本身。

這裏正在使用的代碼,以使該請求:

string postData = String.Format("serviceType={0}&SaleID={1}&Zip={2}", request.service, request.saleId, request.postalCode); 
byte[] data = (new ASCIIEncoding()).GetBytes(postData); 

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
httpWebRequest.Timeout = 60000; 
httpWebRequest.Method = "POST"; 
httpWebRequest.ContentType = "application/x-www-form-urlencoded"; 
httpWebRequest.ContentLength = data.Length; 

using (Stream newStream = httpWebRequest.GetRequestStream()) 
{ 
    newStream.Write(data, 0, data.Length); 
} 

try 
{ 
    using (response = (HttpWebResponse)httpWebRequest.GetResponse()) 
    { 
     if (response.StatusCode != HttpStatusCode.OK) 
      throw new Exception("There was an error with the shipping freight service."); 

     string responseData; 
     using (StreamReader responseStream = new  StreamReader(httpWebRequest.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("iso-8859-1"))) 
     { 
      responseData = responseStream.ReadToEnd(); 
      responseStream.Close(); 
     } 

     if (string.IsNullOrEmpty(responseData)) 
      throw new Exception("There was an error with the shipping freight service. Request went through but response is empty."); 

     XmlDocument providerResponse = new XmlDocument(); 
     providerResponse.LoadXml(responseData); 

     return providerResponse; 
    } 
} 
catch (WebException webExp) 
{ 
    string exMessage = webExp.Message; 

    if (webExp.Response != null) 
    { 
     using (StreamReader responseReader = new StreamReader(webExp.Response.GetResponseStream())) 
     { 
      exMessage = responseReader.ReadToEnd(); 
     } 
    } 

    throw new Exception(exMessage); 
} 

任何人都有一個想法可能是這樣嗎?

謝謝。

UPDATE

通過調試器,我看到參數是正確的。我也看到在提琴手中的參數是正確的。

檢查fiddler,每次執行代碼時都會收到2個請求。第一個請求是發送參數的帖子。它會得到一個301響應代碼,其中包含一個「Document Moved Object Moved此文檔可能在此處」的消息。第二個請求是對沒有正文的同一個URL的GET。 「缺少參數:serviceType」會導致500服務器錯誤。信息。

+1

只要構建Web請求,一切看起來都不錯。我猜這個顯而易見的問題是,「你確定'request.service'不是null或者當你構建postData的時候是一個無效值?」 –

+0

在後臺運行Fiddler並捕獲請求/響應以驗證發送的請求是否有效。 –

+0

是的,我通過visual studio中的調試器。也看過小提琴......一切都很好看。 –

回答

0

看起來你發現你的問題,當你看着小提琴手的請求。以摘錄自http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

10.3.2 301 Moved Permanently

The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible.

.....

Note: When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request.

這裏,你可以採取幾個選項:

  1. 硬編碼程序中使用,你在提琴手
  2. 301響應看到新網址調整您的代碼以檢索301響應,從響應中解析出新的Url,然後使用新的Url構建新的響應。

如果您正在處理Url上的基於用戶的輸入(如網絡瀏覽器),後一個選項將是理想的,因爲您不知道用戶要在哪裏使用程序。

+0

1/2。 301重定向到同一個網址。由於第二個請求是.NET運行時自動執行的,我只是認爲這是.NET運行時如何用於asmx服務或處理錯誤,但我可能是錯誤的。重定向不是問題,上述代碼通過錯誤的「缺少參數」消息獲得響應是一個問題。該服務是標準的.NET asmx服務。上面的代碼是C#中的樣板POST請求。 –

+0

@IanHerbert我能想到的最後一件事可能是您的請求中某些缺失的標頭會導致服務發出錯誤。實際上,你發佈的第一個代碼應該沒問題(你已經知道了)。如果您已經在HttpWebRequest中複製了Web表單請求字符替換字符,並且它仍然不起作用,那麼最後要看的是服務本身(您已經說過這是不可能的)。這是關於我在哪裏的想法。 –

+0

是的,我認爲,除非我可以獲得服務,否則我將無法做更多有用的調試。謝謝。 –

相關問題