San*_*dak 13 asp.net rest android asp.net-web-api android-volley
我正在尝试使用Volley库与我的RESTful API进行通信.
当我要求持票人令牌时,我必须在正文中发布字符串.字符串应如下所示:grant_type = password&username = Alice&password = password123标题:Content-Type: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;
}
};
Run Code Online (Sandbox Code Playgroud)
我一直收到400 Bad Request.我想我实际上发送的请求是这样的:
grant_type:password, username:User0, password:Password0
Run Code Online (Sandbox Code Playgroud)
代替:
grant_type=password&username=Alice&password=password123
Run Code Online (Sandbox Code Playgroud)
如果有人有任何想法或建议,我将非常感激.
geo*_*sey 27
要使用用户名和密码等参数发送普通POST请求(无JSON),您通常会覆盖getParams()并传递参数Map:
public void HttpPOSTRequestWithParameters() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com/login.asp";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR","error => "+error.toString());
}
}
) {
// this is the relevant method
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
// volley will escape this for you
params.put("randomFieldFilledWithAwkwardCharacters", "{{%stuffToBe Escaped/");
params.put("username", "Alice");
params.put("password", "password123");
return params;
}
};
queue.add(postRequest);
}
Run Code Online (Sandbox Code Playgroud)
要在Volley StringRequest 中将任意字符串作为POST正文数据发送,您可以覆盖getBody()
public void HttpPOSTRequestWithArbitaryStringBody() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com/login.asp";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR","error => "+error.toString());
}
}
) {
// this is the relevant method
@Override
public byte[] getBody() throws AuthFailureError {
String httpPostBody="grant_type=password&username=Alice&password=password123";
// usually you'd have a field with some values you'd want to escape, you need to do it yourself if overriding getBody. here's how you do it
try {
httpPostBody=httpPostBody+"&randomFieldFilledWithAwkwardCharacters="+URLEncoder.encode("{{%stuffToBe Escaped/","UTF-8");
} catch (UnsupportedEncodingException exception) {
Log.e("ERROR", "exception", exception);
// return null and don't pass any POST string if you encounter encoding error
return null;
}
return httpPostBody.getBytes();
}
};
queue.add(postRequest);
}
Run Code Online (Sandbox Code Playgroud)
另外,Volley文档不存在,StackOverflow答案的质量非常糟糕.不敢相信这样的例子的答案已经不存在了.
首先,我建议你通过打印到日志或使用wirehark或fiddler等网络嗅探器来确切地看到你发送的内容.
怎么样试图将params放入体内?如果你还想要一个StringRequest你需要扩展它并覆盖getBody()方法(类似于JsonObjectRequest)
| 归档时间: |
|
| 查看次数: |
30391 次 |
| 最近记录: |