我正在开发Windows Phone 7.1最低需求的后台应用程序。它使用asmx webservice。但是我想让应用程序能够连接到任何客户端的服务器。所以我需要每次改变webservice路径以保持方法相同。有没有办法直接在Windows Phone 7中调用websevice而无需添加Web引用?调用WebService不在c#.net框架2.0中添加Web引用Windows Phone 7
示例代码:
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);
}
错误:
'System.Net.WebHeaderCollection' 不包含对 '添加' 的定义和没有扩展方法 '添加' 接受 第一可以找到'System.Net.WebHeaderCollection'类型的参数(您是否缺少使用指令或程序集引用?)
'系统。 Net.HttpWebRequest'不包含'GetRequestStream'的定义,并且没有找到接受'System.Net.HttpWebRequest'类型的第一个参数的扩展方法'GetRequestStream'(你是否缺少使用指令或程序集引用?)
你可以通过使用需要URL的HTTP请求来实现这个......我不会建议这个,但是(你必须确保asmx服务是http get和post启用的。)我宁愿建议制作一个他已经拥有的服务.. – Jonny 2015-01-21 10:13:43
@Jonny我开发了一个类,通过它我可以在不添加web引用的情况下调用webservice,因为windows phone 7使用.net框架2.0,它给了我一些错误。检查以下链接 http://www.diogonunes.com/blog/calling-a-web-method-in-c-without-a-service-reference/ – 2015-01-21 10:23:10
@sumit_dev你可以发布你得到的错误吗?我可以帮忙。我认为你的课程与链接中的代码相同。 – Jonny 2015-01-21 13:19:20