0

我有一个用java编写的网络服务。我从 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>

提前致谢。

4

4 回答 4

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' 指的是设备本身,而不是您的计算机。更多信息

添加服务参考窗口 有了这个,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;
    }

结果如下:

在此处输入图像描述

我希望这很有用。

于 2013-10-31T18:38:02.643 回答
2

如果您在某处有适当的 Web 服务,则可以使用 Visual Studio 生成包装器代码以访问该 Web 服务。查看此链接以了解如何操作。

于 2013-07-01T16:48:21.803 回答
1

最后我得到了完美的答案。通过使用下面的代码,我使用 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);
                }
            }
        }
于 2013-07-10T03:19:39.830 回答
1

希望这会帮助你。

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();
}
于 2013-07-02T05:44:37.413 回答