SOAP Parsing in windows phone 7

2019-08-21 13:56发布

I will searching since 10 days but i have not succeed in soap parsing in wp7.

My code is below. I get the The remote server returned an error: NotFound. and System.Net.WebException.

code is below :

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

I get the error in HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); line...

So, Please help me.

Thanks.

3条回答
Animai°情兽
2楼-- · 2019-08-21 14:24

If you create of an asmx web service. The first call is incredibly slow.

查看更多
We Are One
3楼-- · 2019-08-21 14:30

Maybe I am missing something but why not just adding a service reference ?

The service located at 'http://manarws.org/WS/manarService.asmx' is a classic web service and you can browse wsdl. You can add a reference in Visual Studio. It will generate a proxy class to call this webservice. Manual soap parsing is quite painful.

EDIT :

1) Right clic on service reference in your project.

step1

2) Enter your service url. Then click Go.

step2

3) You will have new classes in your project.

Just use them as you want. Exemple :

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
}
查看更多
看我几分像从前
4楼-- · 2019-08-21 14:43

Follow these steps to know how to use a SOAP service

-- 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.

Now that your proxy classes should be ready.
Go to your Mainpage.xaml.cs and type 'client' there..you should probably get a class with the name "ManarServiceClient".

If you get that, then try to call the suitable methods of that class.

For an example,

ManarServiceClient client = new ManarServiceClient();
client.fnGetNewsResponseCompleted += new EventHandler<fnGetNewsResponseCompletedEventArgs>(client_fnGetNewsResponseCompleted);
client.fnGetNewsResponseAsync();

Note: I am not with my working system, so cannot give you exact code. All the above is a guessed code and shall point you in the right direction. Will test my code and update soon.

查看更多
登录 后发表回答