繁体   English   中英

在Windows Phone 7的c#.net Framework 2.0中调用WebService而不添加Web引用

[英]Calling WebService Without adding web reference in c#.net framework 2.0 for windows phone 7

我正在为Windows Phone 7.1最低要求开发后台应用程序。 它为此消耗了asmx Web服务。 但是我想使应用程序能够连接到任何客户端的服务器。 因此,我每次都必须更改方法,以保持相同的方法更改Web服务路径。 有什么方法可以直接在Windows Phone 7中直接调用websevice,而无需添加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);
}

错误:

  1. 'System.Net.WebHeaderCollection'不包含'Add'的定义,找不到扩展方法'Add'接受类型为'System.Net.WebHeaderCollection'的第一个参数(您是否缺少using指令或程序集引用?)

  2. 'System.Net.HttpWebRequest'不包含'GetRequestStream'的定义,并且找不到扩展方法'GetRequestStream'接受类型为'System.Net.HttpWebRequest'的第一个参数(您是否缺少using指令或程序集引用?)

如果Web服务都是相同的,但它们之间的唯一区别是路径,则可以正常创建服务引用,并在构造函数中分配绑定名称和新的HTTP Uri,如下所示

var serviceClient = new ServiceClient("http", txtUriPath.Text);

但是,如果您有用于每个Web服务的不同Uri路径的不同方法,则必须让我们知道您实际遇到的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM