我正在使用一个简单的 .NET 服务 (asmx),它在通过测试表单 (POST) 调用时可以正常工作。通过 HttpWebRequest 对象调用时,我收到 WebException “System.Net.WebException:远程服务器返回错误:(500)内部服务器错误。” 深入挖掘,阅读 WebException.Response.GetResponseStream() 我收到消息:“缺少参数:serviceType。” 但我已经清楚地包含了这个参数。
我在这里不知所措,更糟糕的是我无权调试服务本身。
这是用于发出请求的代码:
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);
}
有人知道会发生什么吗?
谢谢。
更新
单步调试器,我看到参数是正确的。我还看到提琴手中的参数是正确的。
检查提琴手,每次执行此代码时我都会收到 2 个请求。第一个请求是发送参数的帖子。它会收到一个 301 响应代码,其中包含“文档已移动对象已移动,此文档可以在此处找到”消息。第二个请求是对没有正文的相同 URL 的 GET。它收到一个带有“缺少参数:serviceType”的 500 服务器错误。信息。