我必须调用一个Web服务。 Web 服务具有输入两个参数,并且不会在输出中返回任何内容。
你能告诉我代码(写在下面(是否正确和完整吗? 我把它插入主电源内。
var httpWebRequest = (HttpWebRequest)WebRequest.Create("url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = new NetworkCredential("user", "pwd", "domain");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{p1:'Hello'";
string tmpjs = ",p2:'world'}";
json = json + tmpjs;
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
添加服务引用和调用 Web 服务方法会更容易。
然后,您可以像这样访问该服务。
您的代码如下所示:
string json = "{p1:'Hello'";
string tmpjs = ",p2:'world'}";
json = json + tmpjs;
using(YourService service = new YourService())
{
service.Credentials = new NetworkCredential("user", "pwd", "domain");
serivce.YourServiceMethod(json, param2);
}