我正在开发windows 7.1最低要求的后台应用程序。它为此使用了asmx webservice。但是我想使应用程序能够连接到任何客户端的服务器。因此,每次我都需要更改webservice路径,将方法保持不变。有没有任何方法可以直接调用windows 7中的网络服务而不添加web引用??
样本代码:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
req.Headers.Add("SOAPAction", "\"" + Namespace + methodName + "\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
string postValues = "";
if (Convert.ToInt32(Params["number"]) > 0)
{
foreach (var param in Params)
{
if (encode) postValues += string.Format("<{0}>{1}</{0}>", HttpUtility.UrlEncode(param.Key), HttpUtility.UrlEncode(param.Value));
else postValues += string.Format("<{0}>{1}</{0}>", param.Key, param.Value);
}
}
soapStr = string.Format(soapStr, methodName, postValues);
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soapStr);
}
}
using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream()))
{
string result = responseReader.ReadToEnd();
//ResponseSOAP = XDocument.Parse(Utils.UnescapeString(result));
ExtractResult(methodName);
}错误:
发布于 2015-01-22 04:30:12
如果web服务都是相同的,但它们之间唯一的区别是路径,则可以正常地在构造函数中创建服务引用,并在构造函数中分配绑定名称和新的HTTP Uri,如下所示
var serviceClient = new ServiceClient("http", txtUriPath.Text);但是,如果对于每个Web服务的不同Uri路径都有不同的方法,则必须让我们知道实际存在的错误。
https://stackoverflow.com/questions/28064225
复制相似问题