问题描述
我试图用排枪库与我的REST的API进行通信。
I'm trying to use Volley library to communicate with my RESTful API.
我有权发布字符串中的身体,当我要求承载令牌。字符串应该是这样的:grant_type =密码和放大器;用户名=爱丽丝&放大器;密码= password123和头:内容类型:应用程序/ x-WWW的形式urlen codeD
I have to POST string in the body, when I'm asking for the bearer Token. String should look like this:grant_type=password&username=Alice&password=password123And header:Content-Type: application/x-www-form-urlencoded
有关的WebAPI个人帐户更多信息: http://www.asp.net/web -API /综述/安全性/个人账户功能于网络API
More info about WebApi Individual Accounts:http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
不幸的是我无法弄清楚如何解决它。
Unfortunately I can't figure out how can I solve it..
我想是这样的:
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错误的请求所有的时间。我想,我确实发送这样的请求:
I'm getting 400 Bad Request all the time.I think that I'm actually sending request like this:
grant_type:password, username:User0, password:Password0
来代替:
grant_type=password&username=Alice&password=password123
我会很感激,如果任何人有任何意见或建议。
I would be very grateful if anyone has any ideas or an advice..
推荐答案
第一件事情,我劝你看到你被要么打印到日志或使用诸如Wireshark的提琴手或网络嗅探器发送什么。
First thing, I advise you to see exactly what you're sending by either printing to the log or using a network sniffer like wireshark or fiddler.
如何试图把PARAMS在身上?如果你仍然想有一个 StringRequest
你需要扩展它,并覆盖 getBody()
方法(类似于 JsonObjectRequest
)
How about trying to put the params in the body? If you still want a StringRequest
you'll need to extend it and override the getBody()
method (similarly to JsonObjectRequest
)
这篇关于Android的凌空POST字符串中体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!