在我的项目中,我必须访问drupal7提供的restful api。在使用post请求访问api时,我必须在请求的标头中提供令牌和会话ID。我测试了(Poster,firefox插件用于测试restful api),并在Poster的标题部分正确添加Cookie和X-CRSF-Token,服务器重新启动这个json" {" status":0, "消息":"用户名已经存在!输入其他名称。"}"。
这是我的Android代码,用于使用相同的Cookie和X-CRSF-Token访问相同的api请求,但是服务器返回"访问被拒绝匿名用户"。
非常感谢任何建议,
由于
public void StoreCreateAsync(){
dialog = new ProgressDialog(mContext);
dialog.setMessage("Please wait....");
dialog.show();
final String token = pref.getString(Constants.token,"");
final String cookie = pref.getString(Constants.session,"");
//token = "dWGgzRIPAZt9YI48W2DCPny9Fk8WcPh_ifBgdZ5f8x4";
//cookie = "SESSeef809303b051915f45008580b17a51axQHOT-12m7WHfdU6Xa5vkS3SHfZ5Lhuj3Brgp-uAVyo";
// for handle socketTimeoutException
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(3, TimeUnit.MINUTES);
client.setReadTimeout(3, TimeUnit.MINUTES);
//for header parameter
//method 1
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Cookie", "SESSeef809303b051915f45008580b17a51axQHOT-12m7WHfdU6Xa5vkS3SHfZ5Lhuj3Brgp-uAVyo");
request.addHeader("X-CSRF-Token", "dWGgzRIPAZt9YI48W2DCPny9Fk8WcPh_ifBgdZ5f8x4");
}
};
para user_para = new para();
user_para.storeName = txtName.getText().toString();
restAdapter = new RestAdapter.Builder()
.setEndpoint(Constants.BaseUrl)
.setRequestInterceptor(requestInterceptor)
.setClient(new OkClient(client))
.setLogLevel(RestAdapter.LogLevel.FULL).build();
ApiStoreCreate apiStoreCreate = restAdapter.create(ApiStoreCreate.class);
apiStoreCreate.postUserStore(user_para,new Callback<obj_store_create>(){
@Override
public void success(obj_store_create obj_store_create, Response response) {
dialog.dismiss();
if(response.getStatus() == 200){
Toast.makeText(mContext, obj_store_create.message, Toast.LENGTH_SHORT).show();
edit.putString(Constants.storeName, obj_store_create.storeName);
edit.apply();
}
}
@Override
public void failure(RetrofitError error) {
dialog.dismiss();
response = error.getResponse();
Log.i(tag, String.valueOf(response.getStatus()));
if(response.getStatus() != 200) {
Toast.makeText(mContext, "Error Status -> "+ response.getStatus() + ". Error -> " + response.getReason() ,Toast.LENGTH_SHORT).show();
}
}
});
12-17 11:52:09.950: D/Retrofit(27129): ---> HTTP POST http://127.0.0.1/api/v1/store.json
12-17 11:52:09.950: D/Retrofit(27129): Cookie: SESSeef809303b051915f45008580b17a51axQHOT-12m7WHfdU6Xa5vkS3SHfZ5Lhuj3Brgp-uAVyo
12-17 11:52:09.950: D/Retrofit(27129): X-CSRF-Token: dWGgzRIPAZt9YI48W2DCPny9Fk8WcPh_ifBgdZ5f8x4
12-17 11:52:09.950: D/Retrofit(27129): Content-Type: application/json; charset=UTF-8
12-17 11:52:09.950: D/Retrofit(27129): Content-Length: 26
12-17 11:52:09.950: D/Retrofit(27129): {"storeName":"sawyyyyyuu"}
12-17 11:52:09.950: D/Retrofit(27129): ---> END HTTP (26-byte body)
12-17 11:52:15.160: D/Retrofit(27129): <--- HTTP 403 http://127.0.0.1/api/v1/store.json (5215ms)
12-17 11:52:15.160: D/Retrofit(27129): Date: Thu, 17 Dec 2015 05:22:31 GMT
12-17 11:52:15.160: D/Retrofit(27129): Server: Apache/2.4.9 (Win32) OpenSSL/0.9.8y PHP/5.4.27
12-17 11:52:15.160: D/Retrofit(27129): X-Powered-By: PHP/5.4.27
12-17 11:52:15.160: D/Retrofit(27129): Expires: Sun, 19 Nov 1978 05:00:00 GMT
12-17 11:52:15.160: D/Retrofit(27129): Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
12-17 11:52:15.170: D/Retrofit(27129): Content-Length: 36
12-17 11:52:15.170: D/Retrofit(27129): Keep-Alive: timeout=5, max=100
12-17 11:52:15.170: D/Retrofit(27129): Connection: Keep-Alive
12-17 11:52:15.170: D/Retrofit(27129): Content-Type: application/json
12-17 11:52:15.170: D/Retrofit(27129): OkHttp-Selected-Protocol: http/1.1
12-17 11:52:15.170: D/Retrofit(27129): OkHttp-Sent-Millis: 1450329730132
12-17 11:52:15.170: D/Retrofit(27129): OkHttp-Received-Millis: 1450329735171
12-17 11:52:15.170: D/Retrofit(27129): ["Access denied for user anonymous"]
12-17 11:52:15.170: D/Retrofit(27129): <--- END HTTP (36-byte body)
答案 0 :(得分:0)
我认为您的api正在关注OAuth,他们知道您提出请求的设备类型。这就是为什么它没有识别您在请求中传递的令牌。
如果您想看到请求与&gt;之间的明显差异 1.来自网络的请求 2.来自移动的请求
使用RequestBin它将为您提供终点,当您发出请求时,只需使用RequestBin提供的终点替换您的实际终点。 在执行您的请求后,您可以使用以下所有内容检查已解雇的请求: - RequestBin站点上的标题,正文和内容类型在获得结束点等待并关闭诊断您的请求后不会关闭浏览器。 你可以查看你实际犯错的地方。