0

我将从 10 天开始搜索,但我在 wp7 中的肥皂解析中没有成功。

我的代码如下。我得到远程服务器返回错误:未找到。和 System.Net.WebException。

代码如下:

 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();
            }
        }
    }

我得到了HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);在线错误...

所以,请帮助我。

谢谢。

4

3 回答 3

3

也许我遗漏了一些东西,但为什么不只是添加一个服务参考?

位于“ http://manarws.org/WS/manarService.asmx ”的服务是经典的Web 服务,您可以浏览wsdl。您可以在 Visual Studio 中添加引用。它将生成一个代理类来调用此 Web 服务。手动肥皂解析是相当痛苦的。

编辑 :

1)右键单击项目中的服务参考。

第1步

2) 输入您的服务网址。然后点击前往。

第2步

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
}
于 2012-10-08T13:31:06.610 回答
1

按照以下步骤了解如何使用 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();

注意:我没有使用我的工作系统,所以不能给你确切的代码。以上所有内容都是猜测的代码,将为您指明正确的方向。将测试我的代码并尽快更新。

于 2012-10-08T13:51:42.247 回答
0

如果您创建一个 asmx Web 服务。第一次通话非常慢。

于 2013-11-23T23:45:41.663 回答