【发布时间】:2014-09-24 14:51:11
【问题描述】:
我正在使用开源项目中的 TokenHttpRequest 类:
public String doHTTPRequest(String url){
String responseBody = "";
String token = "";
DefaultHttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpGet httpPost = new HttpGet(url);
try {
ResponseHandler<String> responseHandler=new BasicResponseHandler();
HttpContext context = new BasicHttpContext(); context.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0");
responseBody = httpClient.execute(httpPost, responseHandler, context);
JSONObject jObject = new JSONObject(responseBody);
token = jObject.getString("token");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
return token;
}
在我访问的 moodle 站点更新到 2.7 之前,它运行良好。
现在httpClient.execute 行给出了错误 Forbidden 403。在浏览器中,url 工作正常。
这是我得到的:
09-24 17:42:58.246: W/System.err(8820): org.apache.http.client.HttpResponseException: Forbidden
09-24 17:42:58.246: W/System.err(8820): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
09-24 17:42:58.246: W/System.err(8820): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
09-24 17:42:58.246: W/System.err(8820): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
09-24 17:42:58.246: W/System.err(8820): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
09-24 17:42:58.246: W/System.err(8820): at moodle.android.moodle.helpers.TokenHttpRequest.doHTTPRequest(TokenHttpRequest.java:86)
09-24 17:42:58.246: W/System.err(8820): at ro.example.app.Login$2.run(Login.java:170)
09-24 17:42:58.246: W/System.err(8820): at java.lang.Thread.run(Thread.java:841)
还有:
09-24 17:13:51.986: I/entity(2975): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
09-24 17:13:51.986: I/entity(2975): <html><head>
09-24 17:13:51.986: I/entity(2975): <title>403 Forbidden</title>
09-24 17:13:51.986: I/entity(2975): </head><body>
09-24 17:13:51.986: I/entity(2975): <h1>Forbidden</h1>
09-24 17:13:51.986: I/entity(2975): <p>You don't have permission to access /login/token.php
09-24 17:13:51.986: I/entity(2975): on this server.</p>
09-24 17:13:51.986: I/entity(2975): </body></html>
当我尝试使用EntityUtils获取实体时
有人遇到过这个问题吗?
【问题讨论】: