0

我正在将 WebAppication 重写为 Windows Phone 上的本机应用程序。

我遇到了一些麻烦,无法发布数据。

这是 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 回答