今天在开发微博授权登录是碰到的一个问题:
com.zhiwei.weibo4j.model.WeiboException: weiboreach.database: weiboreach.database
at com.zhiwei.weibo4j.http.HttpClient.httpRequest(HttpClient.java:425)
at com.zhiwei.weibo4j.http.HttpClient.httpRequest(HttpClient.java:374)
at com.zhiwei.weibo4j.http.HttpClient.get(HttpClient.java:206)
at com.zhiwei.weibo4j.Users.showUserById(Users.java:30)
at com.zhiwei.authUser.dao.impl.AuthUserDaoImpl.addAuthUser(AuthUserDaoImpl.java:49)
at com.zhiwei.servlet.WeiboServlet.doGet(WeiboServlet.java:39)
at com.zhiwei.servlet.WeiboServlet.doPost(WeiboServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.net.UnknownHostException: weiboreach.database: weiboreach.database
at java.net.InetAddress.getLocalHost(InetAddress.java:1353)
at com.zhiwei.weibo4j.http.HttpClient.httpRequest(HttpClient.java:382)
上网百度后搜到这样一条帖子:http://www.tuicool.com/articles/FZJfAz2
说的是机器的回环地址问题,原谅我的半路出家,基本没怎么看明白。后来还是按照错误提示最后找到了这里
public Response httpRequest(HttpMethod method, Boolean WithTokenHeader)
throws WeiboException {
InetAddress ipaddr;
int responseCode = -1;
try {
// ipaddr = InetAddress.getLocalHost();
List<Header> headers = new ArrayList<Header>();
if (WithTokenHeader) {
if (token == null) {
throw new IllegalStateException("Oauth2 token is not set!");
}
headers.add(new Header("Authorization", "OAuth2 " + token));
//
headers.add(new Header("API-RemoteIP", ipaddr.getHostAddress()));
client.getHostConfiguration().getParams()
.setParameter("http.default-headers", headers);
for (Header hd : headers) {
log(hd.getName() + ": " + hd.getValue());
}
}
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
client.executeMethod(method);
Header[] resHeader = method.getResponseHeaders();
responseCode = method.getStatusCode();
log("Response:");
log("https StatusCode:" + String.valueOf(responseCode));
for (Header header : resHeader) {
log(header.getName() + ":" + header.getValue());
}
Response response = new Response();
response.setResponseAsString(method.getResponseBodyAsString());
log(response.toString() + "\n");
if (responseCode != OK)
{
try {
throw new WeiboException(getCause(responseCode),
response.asJSONObject(), method.getStatusCode());
} catch (JSONException e) {
e.printStackTrace();
}
}
return response;
} catch (IOException ioe) {
throw new WeiboException(ioe.getMessage(), ioe, responseCode);
} finally {
method.releaseConnection();
}
}
问题出在headers.add(new Header("API-RemoteIP", ipaddr.getHostAddress()));这个位置,你可以按照上面的博客改动测试下,也可以直接把这的ip换成你服务器的ip地址测试下