windowsphone7中的SOAP解析
我将从10天开始搜索,但我没有在wp7中成功进行soap解析 我的代码如下。我收到远程服务器返回的错误消息:NotFound。和System.Net.WebException 代码如下:windowsphone7中的SOAP解析,windows,windows-phone-7,Windows,Windows Phone 7,我将从10天开始搜索,但我没有在wp7中成功进行soap解析 我的代码如下。我收到远程服务器返回的错误消息:NotFound。和System.Net.WebException 代码如下: private const string AuthServiceUri = "http://manarws.org/WS/manarService.asmx"; private const string AuthEnvelope = @"<?xml
private const string AuthServiceUri = "http://manarws.org/WS/manarService.asmx";
private const string AuthEnvelope =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<fnGetNewsResponse xmlns=""http://tempuri.org/"">
<fnGetNewsResult></fnGetNewsResult>
</fnGetNewsResponse>
</soap:Body>
</soap:Envelope>";
public void Authenticate()
{
HttpWebRequest spAuthReq = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest;
spAuthReq.Headers["SOAPAction"] = "http://tempuri.org/fnGetNews";
spAuthReq.ContentType = "text/xml; charset=utf-8";
spAuthReq.Method = "POST";
spAuthReq.BeginGetRequestStream(new AsyncCallback(spAuthReqCallBack), spAuthReq);
}
private void spAuthReqCallBack(IAsyncResult asyncResult)
{
UTF8Encoding encoding = new UTF8Encoding();
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
System.Diagnostics.Debug.WriteLine("REquest is :" + request.Headers);
Stream _body = request.EndGetRequestStream(asyncResult);
string envelope = string.Format(AuthEnvelope,"","");
System.Diagnostics.Debug.WriteLine("Envelope is :" + envelope);
byte[] formBytes = encoding.GetBytes(envelope);
_body.Write(formBytes, 0, formBytes.Length);
_body.Close();
request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
}
private void ResponseCallback(IAsyncResult asyncResult)
{
System.Diagnostics.Debug.WriteLine("Async Result is :" + asyncResult);
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
System.Diagnostics.Debug.WriteLine("Response is :::::::::::::::::::----" + request.EndGetResponse(asyncResult));
if (request != null && response != null)
{
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
}
}
}
私有常量字符串AuthServiceUri=”http://manarws.org/WS/manarService.asmx";
私有常量字符串身份验证信封=
@"
";
公共无效身份验证()
{
HttpWebRequest spAuthReq=HttpWebRequest.Create(AuthServiceUri)作为HttpWebRequest;
spAuthReq.Headers[“SOAPAction”]=”http://tempuri.org/fnGetNews";
spAuthReq.ContentType=“text/xml;charset=utf-8”;
spAuthReq.Method=“POST”;
BeginGetRequestStream(新的异步回调(spAuthReqCallBack),spAuthReq);
}
私有void spAuthReqCallBack(IAsyncResult asyncResult)
{
UTF8Encoding=新的UTF8Encoding();
HttpWebRequest请求=(HttpWebRequest)asyncResult.AsyncState;
System.Diagnostics.Debug.WriteLine(“请求为:“+REquest.Headers”);
Stream _body=request.EndGetRequestStream(asyncResult);
字符串信封=string.Format(AuthEnvelope,“,”);
System.Diagnostics.Debug.WriteLine(“信封是:“+信封”);
字节[]formBytes=编码.GetBytes(信封);
_写入(formBytes,0,formBytes.Length);
_body.Close();
BeginGetResponse(新的AsyncCallback(ResponseCallback),request);
}
私有void ResponseCallback(IAsyncResult异步结果)
{
System.Diagnostics.Debug.WriteLine(“异步结果为:”+asyncResult);
HttpWebRequest请求=(HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse=(HttpWebResponse)request.EndGetResponse(asyncResult);
System.Diagnostics.Debug.WriteLine(“响应是::::::::----”+request.EndGetResponse(asyncResult));
if(请求!=null和响应!=null)
{
if(response.StatusCode==HttpStatusCode.OK)
{
StreamReader=新的StreamReader(response.GetResponseStream());
字符串responseString=reader.ReadToEnd();
}
}
}
我在HttpWebResponse=(HttpWebResponse)request.EndGetResponse(asyncResult)中得到错误代码>行
所以,请帮帮我
谢谢。也许我遗漏了什么,但为什么不添加一个服务引用呢
位于“”的服务是典型的web服务,您可以浏览wsdl。您可以在VisualStudio中添加引用。它将生成一个代理类来调用此webservice。手动soap解析是相当痛苦的
编辑:
1) 在项目中的服务引用上使用右clic
2) 输入您的服务url。然后单击Go
3) 您的项目中将有新的类
你想用就用吧。例如:
public void GetBranches()
{
ManarServiceReference.manarServiceSoapClient client = new ManarServiceReference.manarServiceSoapClient();
client.fnGetBranchesCompleted += new EventHandler<ManarServiceReference.fnGetBranchesCompletedEventArgs>(client_fnGetBranchesCompleted);
client.fnGetBranchesAsync();
}
void client_fnGetBranchesCompleted(object sender, ManarServiceReference.fnGetBranchesCompletedEventArgs e)
{
//TODO
}
public void getbranchs()
{
ManarServiceReference.manarServiceSoapClient=新的ManarServiceReference.manarServiceSoapClient();
client.fnGetBranchesCompleted+=新事件处理程序(client_fnGetBranchesCompleted);
client.fGetBranchesAsync();
}
无效客户端\u fGetBranchesCompleted(对象发送方,ManarServiceReference.fGetBranchesCompletedEventArgs e)
{
//待办事项
}
也许我遗漏了一些东西,但为什么不添加一个服务引用呢
位于“”的服务是典型的web服务,您可以浏览wsdl。您可以在VisualStudio中添加引用。它将生成一个代理类来调用此webservice。手动soap解析是相当痛苦的
编辑:
1) 在项目中的服务引用上使用右clic
2) 输入您的服务url。然后单击Go
3) 您的项目中将有新的类
你想用就用吧。例如:
public void GetBranches()
{
ManarServiceReference.manarServiceSoapClient client = new ManarServiceReference.manarServiceSoapClient();
client.fnGetBranchesCompleted += new EventHandler<ManarServiceReference.fnGetBranchesCompletedEventArgs>(client_fnGetBranchesCompleted);
client.fnGetBranchesAsync();
}
void client_fnGetBranchesCompleted(object sender, ManarServiceReference.fnGetBranchesCompletedEventArgs e)
{
//TODO
}
public void getbranchs()
{
ManarServiceReference.manarServiceSoapClient=新的ManarServiceReference.manarServiceSoapClient();
client.fnGetBranchesCompleted+=新事件处理程序(client_fnGetBranchesCompleted);
client.fGetBranchesAsync();
}
无效客户端\u fGetBranchesCompleted(对象发送方,ManarServiceReference.fGetBranchesCompletedEventArgs e)
{
//待办事项
}
按照以下步骤了解如何使用SOAP服务
-- Create a new project.
-- Right-click on the Project name and click on "Add Service Reference"...
Then provide address as "http://manarws.org/WS/manarService.asmx?wsdl" and click Go.
-- Once service information is downloaded, provide Namespace something like
"MyMemberService" at the bottom and click Ok.
现在您的代理类应该准备好了。
转到Mainpage.xaml.cs并在那里键入'client'。您可能会得到一个名为“ManarServiceClient”的类
如果你得到了,那么试着调用该类的合适方法
比如,
ManarServiceClient client = new ManarServiceClient();
client.fnGetNewsResponseCompleted += new EventHandler<fnGetNewsResponseCompletedEventArgs>(client_fnGetNewsResponseCompleted);
client.fnGetNewsResponseAsync();
ManarServiceClient=newmanarserviceclient();
client.fnGetNewsResponseCompleted+=新事件处理程序(client\u fnGetNewsResponseCompleted);
client.fnGetNewsResponseAsync();
注意:我不支持我的工作系统,因此无法提供准确的代码。以上所有这些都是猜测的代码,将为您指明正确的方向。将测试我的代码并很快更新。按照以下步骤了解如何使用SOAP服务
-- Create a new project.
-- Right-click on the Project name and click on "Add Service Reference"...
Then provide address as "http://manarws.org/WS/manarService.asmx?wsdl" and click Go.
-- Once service information is downloaded, provide Namespace something like
"MyMemberService" at the bottom and click Ok.
现在您的代理类应该准备好了。
转到Mainpage.xaml.cs并在那里键入'client'。您可能会得到一个名为“ManarServiceClient”的类
如果你得到了,那么试着调用该类的合适方法
比如,
ManarServiceClient client = new ManarServiceClient();
client.fnGetNewsResponseCompleted += new EventHandler<fnGetNewsResponseCompletedEventArgs>(client_fnGetNewsResponseCompleted);
client.fnGetNewsResponseAsync();
ManarServiceClient=newmanarserviceclient();
client.fnGetNewsResponseCompleted+=新事件处理程序(client\u fnGetNewsResponseCompleted);
client.fnGetNewsResponseAsync();
注意:我不支持我的工作系统,因此无法提供准确的代码。以上所有这些都是猜测的代码,将为您指明正确的方向。将测试我的代码并很快更新。如果您创建了一个asmx web服务。第一个电话的速度非常慢