日期:2014-05-20  浏览次数:20699 次

httpclient返回302后,其location的值为./ 跳转出错,请大侠帮着看看是什么原因。
代码如下:

client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
"utf-8");
String logUrl = "http://192.168.2.38:8812/emlog/admin/index.php?action=login";
postMethod = new PostMethod(logUrl);
postMethod.setRequestHeader("Referer",
"http://192.168.2.38:8812/emlog/admin/");
postMethod.setRequestHeader("ontent-Type",
"application/x-www-form-urlencoded");

NameValuePair[] logData = { new NameValuePair("user", "lzw"),
new NameValuePair("pw", "lzw123456") };

postMethod.setRequestBody(logData);
int code = client.executeMethod(postMethod);
System.out.println(code);
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
Header location = null;
if (code == HttpStatus.SC_MOVED_PERMANENTLY
|| code == HttpStatus.SC_MOVED_TEMPORARILY
|| code == HttpStatus.SC_SEE_OTHER
|| code == HttpStatus.SC_TEMPORARY_REDIRECT) {
location = postMethod.getResponseHeader("location");
System.out.println(location.getValue());
if(location != null){
String newUrl = location.getValue();
if(newUrl == null || newUrl.equals("")){
newUrl = "/";
}
getMethod = new GetMethod(newUrl);
client.executeMethod(getMethod);
System.out.println("Redirect: " + getMethod.getStatusLine().toString());
}else{
System.out.println("失败");
}
}


报错信息:
java.lang.IllegalArgumentException: host parameter is null
org.apache.commons.httpclient.HttpConnection.setHost(HttpConnection.java:249)
org.apache.commons.httpclient.SimpleHttpConnectionManager.getConnectionWithTimeout(SimpleHttpConnectionManager.java:189)
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:153)
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
demo.struts.JumpAction.loginLog(JumpAction.java:60)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)


------解决方案--------------------
if(location != null){
String newUrl = location.getValue();
if(newUrl == null || newUrl.equals("")){
newUrl = "/";
}
getMethod = new GetMethod(newUrl);

-------------------------------------