2012-10-08 84 views
0

我將在10天后進行搜索,但是我沒有成功在wp7中進行soap解析。在windows phone 7中進行SOAP解析

我的代碼如下。我得到遠程服務器返回一個錯誤:NotFound。和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);線得到錯誤...

所以,請幫助我。

謝謝。

回答

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

注:我不是我的工作系統,所以無法給你確切的代碼。以上所有內容都是一個猜測的代碼,應該指向正確的方向。將測試我的代碼並儘快更新。

+0

添加http://manarws.org/WS/manarService.asmx或http://manarws.org/WS/manarService.asmx?wsdl ?? – Rishi

+0

非常感謝您的支持。它在另一臺Windows機器上完成。 – Rishi

+0

嗨,請告訴我如何解析這個webservie? manarws.org/WS/manarService.asmx?op=fnGetMoreProductPhotos這是一個參數,所以如何將參數發送到webservice? – Rishi

3

也許我錯過了一些東西,但爲什麼不只是添加服務引用?

位於'http://manarws.org/WS/manarService.asmx'的服務是一種經典的Web服務,您可以瀏覽wsdl。您可以在Visual Studio中添加引用。它會生成一個代理類來調用這個web服務。手動肥皂解析非常痛苦。

編輯:

1)在項目服務引用右集團公司。

step1

2)輸入您的服務網址。然後點擊開始。

step2

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 
} 
+0

請把這裏的代碼我困惑bcz我m新的Windows Phone 7的發展。謝謝。 – Rishi

+0

@Rishi好吧,看編輯 – Cybermaxs

+0

好工作Cyber​​maxs。我已upvoted你的答案:) – nkchandra

0

如果您創建了asmx Web服務。第一個電話非常慢。