0

Windows Phone で WebAppication を Native Application に書き直しています。

少しトラブルが発生しました。データを投稿できません。

WebApplication のコードは次のとおりです。動作します。

$.ajax({
        url: 'https://blabla.blabla.com/mobile-api/v1/security',
        contentType: 'application/json',
        // data: JSON.stringify ({"jsonrpc": "2.0", "method": "login", "params":["TestLogin", "SuperPass"], "id": 1}),
        data: JSON.stringify({"jsonrpc": "2.0", "method": "login", "params":[username, password], "id": 1}),  
        type:"POST",
        success: function(data){
            callback.onSuccess(data);
        },
        error: function(xhr){
            callback.onError(xhr.status);
        }
    });

そして、ここに私のC#コードがあります:

private void PostLoginData()
    {
        HttpWebRequest request = HttpWebRequest.CreateHttp("https://blabla.blabla.com/mobile-api/v1/security");
        request.Method="POST";
        request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),request);
    }
    void GetRequestStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
        // End the stream request operation
        Stream postStream = myRequest.EndGetRequestStream(callbackResult);

        // Create the post data
        string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":{\"username\":" + "\"TestLogin\"" + ", \"password\":" + "\"SuperPass\"" + "}, \"id\": 1}";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Add the post data to the web request
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        // Start the web request
        myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
    }
    void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
        using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
        {
            string result = httpWebStreamReader.ReadToEnd();
            //For debug: show results
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(result);
            });
        }
    }

応答は私を返します:

メソッド パラメータが無効です。

「」なしでログインとパスワードを投稿しようとすると、次のようになります。

string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":{\"username\":" + "TestLogin" + ", \"password\":" + "SuperPass" + "}, \"id\": 1}";

応答は次のとおりです。

パースエラー

ログインとパスワードは 100% 正しいです... WebApplications リクエストが機能するため、投稿データに問題があると思います

4

1 に答える 1

1

正しい文字列は次のとおりです。

string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":[" + "\""+Username+"\"" + "," + "\""+UserPassword+"\"" + "], \"id\": 1}";
于 2013-07-19T12:07:08.330 に答える