【问题标题】:web service call in windows phone 8Windows Phone 8 中的 Web 服务调用
【发布时间】:2013-11-01 02:38:02
【问题描述】:

我有一个用 java 编写的 web 服务。我从 iPhone 应用程序调用但不知道如何调用窗体 windows 电话。 Web服务具有三个参数用户名、密码和应用程​​序ID。我想通过 HttpWebRequest 调用并接收响应。我该怎么做?

<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
>
-<SOAP-ENV:Body>
-<doLogin xmlns="http://login.mss.uks.com">
-<loginid xsi:type="xsd:string">abc</loginid>
-<password xsi:type="xsd:string">pqrs</password>
-<app_id xsi:type="xsd:int">2</app_id>
</doLogin>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

提前致谢。

【问题讨论】:

    标签: windows-phone-8


    【解决方案1】:

    如果您在某个地方有适当的 Web 服务,则可以使用 Visual Studio 生成包装器代码来访问该 Web 服务。查看this link 了解如何操作。

    【讨论】:

    • 我想使用 HttpWebRequest 调用网络服务。这样我就可以通过编码动态更改 url 并将参数传递给服务器。
    • 您绝对可以通过生成的包装代码传递参数。
    • 我认为通过包装代码Web服务URL不能动态改变。在我的应用程序中,用户可以通过我的应用程序中的任何设置屏幕更改 Web 服务 url。
    • 是的,它不能改变,因为 Web 服务总是与某个基本 URL 相关联。这就是为什么如果您有一组不同的基本 URL,这很可能意味着您有几个不同的 Web 服务。在这种情况下,您应该创建多个包装器。
    • 我没有得到完美的解决方案,即使用 HttpWebRequest。最后我用 HttpWebRequest 解决了我的问题,代码如下。
    【解决方案2】:

    这些天我也一直在为这个话题而苦苦挣扎。这是我的情况以及我是如何解决这个问题的:

    问题

    我已经使用带有 NuSOAP 的 PHP 创建了一个 Web 服务,现在我必须创建一个使用该 Web 服务的 Windows Phone 8 应用程序来做一些事情。

    解决方案

    我将使用示例来解释我的解决方案。

    这是网络服务:

    <?php
    require_once 'lib/nusoap.php';
    
    function test()
    { 
        return "Hello World!";
    }
    
    
    $server = new soap_server();
    $server->configureWSDL("MyService", "urn:MyService");
    
    // IMPORTANT: Avoid some ProtocolException
    $server->soap_defencoding = 'utf-8'; 
    
    $server->register('test',
        array(),
        array('greeting' => 'xsd:string'),
        'Service',
        false,
        'rpc',
        'literal',  // IMPORTANT: 'encoded' isn't compatible with Silverlight
        'A simple hello world'
    );
    
        
    if (isset($HTTP_RAW_POST_DATA)) {
        $server->service($HTTP_RAW_POST_DATA);
    } else {
        $server->service("php://input");
    }
    ?>
    

    现在是 Windows Phone 中的客户端应用程序:

    1. 创建一个新项目。
    2. 创建一个名为“testText”的文本框和一个名为“testButton”的按钮。
    3. 添加服务引用

    解决方案资源管理器 -> MyProjectName -> 添加服务参考。

    注意:如果 Web 服务安装在您的本地计算机上,请不要使用“localhost”作为服务器名称,请使用您的 IP。在 WP 8 中,'localhost' 指的是设备本身,而不是您的计算机。 More info.

    这样,Visual Studio 将自动创建所有需要的类来使用 Web 服务。

    给按钮添加一些动作:

        private void testButton_Click(object sender, RoutedEventArgs e)
        {
            var client = new ServiceReference.MyServicePortTypeClient();
            client.testCompleted += client_testCompleted;
            client.testAsync();
        }
    
        private void client_testCompleted(object sender, ServiceReference.testCompletedEventArgs e)
        {
            testText.Text = e.Result;
        }
    

    结果如下:

    我希望这很有用。

    【讨论】:

      【解决方案3】:

      希望这会对你有所帮助。

      private void Button_Click_1(object sender, RoutedEventArgs e)
      {
          // Create a new HttpWebRequest object.
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.xxx.com/webservicelogin/webservice.asmx/ReadTotalOutstandingInvoice");
      
          request.ContentType = "application/x-www-form-urlencoded";
          request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)";
          request.CookieContainer = cookie;
      
          // Set the Method property to 'POST' to post data to the URI.
          request.Method = "POST";
      
          // start the asynchronous operation
          request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
      
      }
      
      private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
      {
          HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
      
          // End the operation
          Stream postStream = request.EndGetRequestStream(asynchronousResult);
      
          //postData value
          string postData = "xxxxxxxxxx";  
      
          // Convert the string into a byte array. 
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
      
          // Write to the request stream.
          postStream.Write(byteArray, 0, postData.Length);
          postStream.Close();
      
          // Start the asynchronous operation to get the response
          request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
      
      }
      
      private void GetResponseCallback(IAsyncResult asynchronousResult)
      {
      
                  HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
                  // End the operation
      
                  HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
      
                  Stream streamResponse = response.GetResponseStream();
      
                  StreamReader streamRead = new StreamReader(streamResponse);
                  string read = streamRead.ReadToEnd();
      
                  //respond from httpRequest
                  TextBox.Text = read;
      
                  // Close the stream object
                  streamResponse.Close();
                  streamRead.Close();
                  response.Close();
      }
      

      【讨论】:

      • 看不懂这里的cookie是什么?
      • @Rajesh,在自动化某些事情时,您通常需要“登录”以维护与服务器的某种会话/状态。有时这是通过基于表单的身份验证和 cookie 来实现的。您将表单发布到服务器,它会在传入的 HTTP 标头中使用 cookie 进行响应。您需要在后续请求中将此 cookie 传递回服务器以保持状态或保持会话活动。
      【解决方案4】:

      我终于得到了完美的答案。通过使用下面的代码,我使用 HttpWebRequest 解决了我的问题。

                          string url = "http://urlname";
      
      
                          HttpWebRequest request = WebRequest.CreateHttp(new Uri(url)) as HttpWebRequest;
                          request.AllowReadStreamBuffering = true;
                          string strsoaprequestbody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                              "<soap-env:envelope xmlns:soap-env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap-enc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" soap-env:encodingstyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
                              "<soap-env:body>\n" +
                              "<dologin xmlns=\"http://login.mss.uks.com\">\n" +
                              "<username xsi:type=\"xsd:string\">userID</username>\n" +
                              "<password xsi:type=\"xsd:string\">password</password>\n" +
                              "<app_id xsi:type=\"xsd:int\">2</app_id>\n" +
                              "</dologin>" +
                              "</soap-env:body>\n" +
                              "</soap-env:envelope>\n";
      
      
                          request.ContentType = "application/x-www-form-urlencoded";
                          request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)";
                          request.Headers["SOAPAction"] = "http://schemas.xmlsoap.org/soap/encoding/";
                          // Set the Method property to 'POST' to post data to the URI.
                          request.Method = "POST";
                          request.ContentLength = strsoaprequestbody.Length;
                          // start the asynchronous operation
                          request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
                      }
      
      private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
              {
                  Debug.WriteLine("GetRequestStreamCallback method called....");
                  HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
      
                  // End the operation
                  Stream postStream = request.EndGetRequestStream(asynchronousResult);
      
                  string postData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                                      "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
                                      "<SOAP-ENV:Body>\n" +
                                      "<doLogin xmlns=\"http://login.mss.uks.com\">\n" +
                                      "<username xsi:type=\"xsd:string\">userID</username>\n" +
                                      "<password xsi:type=\"xsd:string\">password</password>\n" +
                                      "<app_id xsi:type=\"xsd:int\">2</app_id>\n" +
                                      "</doLogin>" +
                                      "</SOAP-ENV:Body>\n" +
                                      "</SOAP-ENV:Envelope>\n";
      
                  // Convert the string into a byte array. 
                  byte[] byteArray = Encoding.UTF8.GetBytes(postData);
      
                  // Write to the request stream.
                  postStream.Write(byteArray, 0, postData.Length);
                  postStream.Close();
      
                  // Start the asynchronous operation to get the response
                  request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
              }
      
              private static void GetResponseCallback(IAsyncResult asynchronousResult)
              {
                  Debug.WriteLine("GetResponseCallback method called....");
      
                  try
                  {
                      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
      
                      // End the operation
                      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
                      Stream streamResponse = response.GetResponseStream();
                      StreamReader streamRead = new StreamReader(streamResponse);
                      string responseString = streamRead.ReadToEnd();
              //display the web response
                      Debug.WriteLine("Response String : " + responseString);
                      // Close the stream object
                      streamResponse.Close();
                      streamRead.Close();
      
                      // Release the HttpWebResponse
                      response.Close();
                  }
                  catch (WebException ex)
                  {
                      using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
                      {
                          Debug.WriteLine("Exception output : " + ex);
                      }
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多