首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AJAX对HTTPRequest

AJAX对HTTPRequest
EN

Stack Overflow用户
提问于 2013-07-19 11:38:30
回答 1查看 1.1K关注 0票数 0

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

我惹了点小麻烦,不能发布数据。

下面是WebApplication的代码-它可以工作:

代码语言:javascript
复制
$.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#代码:

代码语言:javascript
复制
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);
            });
        }
    }

回复我:

无效方法参数。

如果我试图在没有"“的情况下发布登录和密码,如:

代码语言:javascript
复制
string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":{\"username\":" + "TestLogin" + ", \"password\":" + "SuperPass" + "}, \"id\": 1}";

答复如下:

解析误差

登录和密码100%正确.我认为问题在post数据中,因为WebApplications请求工作

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-19 12:07:08

正确的字符串应该是:

代码语言:javascript
复制
string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":[" + "\""+Username+"\"" + "," + "\""+UserPassword+"\"" + "], \"id\": 1}";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17745216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档