代码如下: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)

解决方案 »

  1.   

    postMethod.setRequestHeader("ontent-Type",
    "application/x-www-form-urlencoded");
    这一句好像写错了。
    应该是:
    postMethod.setRequestHeader("Content-Type",
    "application/x-www-form-urlencoded");
    少写了一个c吧。
      

  2.   

    就算是加上C,还是报java.lang.IllegalArgumentException: host parameter is null
      

  3.   

    location = postMethod.getResponseHeader("location"); // 其返回值为 ./ 而不是带有http://......的什么地址或host,我要怎么改一下才可以
      

  4.   

    if(location != null){
    String newUrl = location.getValue();
    if(newUrl == null || newUrl.equals("")){
    newUrl = "/";
    }
    getMethod = new GetMethod(newUrl);-------------------------------------
    改为:String redirectURL = toRedirectURL(location, postMethod.getURI());
    getMethod = new GetMethod(redirectURL);private String toRedirectURL(String location, URI lastURI) {
        if (location == null || location.trim().length() == 0) {
            location = "/";
        }
        String tmp = location.toLowerCase();
        if (!tmp.startsWith("http://") && !tmp.startsWith("https://")) {
             if (lastUrl == null) {
                 throw new IllegalArgumentsException("lastUrl is null, can not find relative protocol and host name");
             }
             return new URI(lastURI, location, false).toString();
        }
        return location;
    }----------------------------------根据 RFC 2616 -- HTTP/1.1,响应报头 Location 后的 URI 在规范中明确地说明了是一个绝对路径!像这种不规范的 Location 应强烈遣责!RFC 2616 - HTTP/1.1
    14.30 Location
    http://tools.ietf.org/html/rfc2616#section-14.30请求报头 Referer 和响应报头 Content-Location 在 RFC 2616 中说明了既可以使用绝对路径,也可以使用相对路径。