我已经编写了自己的 Web 服务,它可以独立运行。我正在从另一个页面调用此 Web 服务,当时它返回 500 Internal Server 错误。我第一次经历这个过程,不知道这意味着什么或为什么会发生这种情况。在 Google 上进行的初步搜索显示了广泛的答案,尽管我找不到任何具体的答案。
这是我的呼叫代码-
List<string> inputList = new List<string>();
inputList.Add("F");
inputList.Add("B");
List<string> resultList = new List<string>();
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/Helpers/MyService.asmx");
httpWebRequest.Headers.Add("SOAPAction", "\"http://tempuri.org/GetOutput\"");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
JavaScriptSerializer serializer = new JavaScriptSerializer();
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = serializer.Serialize(inputList);
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); *****ERROR HERE
string responseText = String.Empty;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
resultList = serializer.Deserialize<List<string>>(responseText);
这是我的网络服务代码 -
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyService: System.Web.Services.WebService
{
public string GetOutput(string jsonStr)
{
try
{
/*code to format jsonStr and send it along to another web service*/
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string responseText = String.Empty;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
//this obj has a head & body
ResponseObj response_obj = new ResponseObj();
response_obj = serializer.Deserialize<ResponseObj>(responseText);
List<string> Values = new List<string>();
if (response_obj.head.result)
return serializer.Serialize(response_obj.body);
return "";
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}