我正在调用我的 Web 服务的特定方法,但是一旦我创建了一个新的 StreamWriter 对象,就会出现上述错误。我不确定为什么会发生这种情况,因为我以前从未这样做过。我用来执行此操作的代码是使用此链接编写的 - Invoking an ASP.NET web service method via an http request as reference。这是片段 -
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/Helpers/Symbology.asmx");
httpWebRequest.Headers.Add("SOAPAction", "\"http://tempuri.org/GetRelatedSymbols\"");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
JavaScriptSerializer serializer = new JavaScriptSerializer();
*using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))*//error occurs here
{
string json = serializer.Serialize(relatedSymbols);
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string responseText = String.Empty;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
这是我的网络服务代码片段 -
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Symbology: System.Web.Services.WebService
{
[WebMethod]
public List<string> GetRelatedSymbols()
{
try
{
//code here
}
catch(){}
}
}
谁能帮忙指出我可能做错了什么?