我使用簡單的.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服務器錯誤。信息。
只要構建Web請求,一切看起來都不錯。我猜這個顯而易見的問題是,「你確定'request.service'不是null或者當你構建postData的時候是一個無效值?」 –
在後臺運行Fiddler並捕獲請求/響應以驗證發送的請求是否有效。 –
是的,我通過visual studio中的調試器。也看過小提琴......一切都很好看。 –