我正在将WebAppication重写为Windows上的本地应用程序。
我惹了点小麻烦,不能发布数据。
下面是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%正确.我认为问题在post数据中,因为WebApplications请求工作。
发布于 2013-07-19 12:07:08
正确的字符串应该是:
string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":[" + "\""+Username+"\"" + "," + "\""+UserPassword+"\"" + "], \"id\": 1}";https://stackoverflow.com/questions/17745216
复制相似问题