0

私はJavaで書かれたWebサービスを持っています。iPhone アプリケーションから電話をかけましたが、Windows Phone から電話をかける方法がわかりません。Web サービスには、ユーザー名、パスワード、およびアプリケーション ID の 3 つのパラメーターがあります。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 アプリを作成する必要があります。

解決

例を使用して私のソリューションを説明します。

Web サービスは次のとおりです。

<?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」という名前の TextBox と「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 に答える
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 に答える
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 に答える