【发布时间】:2014-02-27 01:10:30
【问题描述】:
我正在尝试使用 Volley 库与我的 RESTful API 进行通信。
当我要求持有者令牌时,我必须在正文中发布字符串。字符串应如下所示: grant_type=密码&用户名=Alice&密码=密码123 和标题: 内容类型:application/x-www-form-urlencoded
有关 WebApi 个人帐户的更多信息: http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
不幸的是,我不知道如何解决它..
我正在尝试这样的事情:
StringRequest req = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
VolleyLog.v("Response:%n %s", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", "User0");
params.put("password", "Password0");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
};
我一直收到 400 错误请求。 我认为我实际上是在发送这样的请求:
grant_type:password, username:User0, password:Password0
代替:
grant_type=password&username=Alice&password=password123
如果有人有任何想法或建议,我将非常感激..
【问题讨论】:
标签: android asp.net rest asp.net-web-api android-volley