2 figurers figurers 于 2017.09.17 19:25 提问

httpclient包使用的问题

这是我的代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class CatchWeb {
static HttpClient client = new HttpClient();

public static void main(String[] args) {
     Init();
}


private static void Init() {
getImage("【我的账号】","【我的密码】");
}

private static void getImage(String name_get, String password_get) {
/**获取验证码图片*/
    GetMethod get = new GetMethod("http://jwxt.gcu.edu.cn/CheckCode.aspx");
    try {
        client.executeMethod(get);
        File storeFile = new File("D:/verifycode.jpg");
        InputStream is = get.getResponseBodyAsStream();
        FileOutputStream fos = new FileOutputStream(storeFile);
        byte[] b = new byte[1024];
        while ((is.read(b)) != -1) {
            fos.write(b);
        }
        is.close();
        fos.close();
        String codeVal = getCode();
        System.out.println(codeVal);
        postRequest(name_get, password_get, codeVal);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void postRequest(String name_data, String password_data,String code) {
    PostMethod postMethod = new PostMethod("http://jwxt.gcu.edu.cn/default2.aspx");
    NameValuePair[] data = { 
    new NameValuePair("__VIEWSTATE", "dDwyODE2NTM0OTg7Oz7c6+5UiE0x3F6KGVy0J/g7vA1NIQ=="),
            new NameValuePair("txtUserName", name_data),
            new NameValuePair("TextBox2", password_data),
            new NameValuePair("txtSecretCode", code),
            new NameValuePair("RadioButtonList1", "学生"),
            new NameValuePair("Button1", "")
    };
    postMethod.setRequestBody(data);
    try {
    client.executeMethod(postMethod);
        String text = postMethod.getResponseBodyAsString();
        System.out.println(text);
        if (text.contains("验证码不正确")) {
            System.out.println("验证码不正确");
        }
        else if(text.contains("密码错误")){
            System.out.println("密码错误");
        }
        else{
        System.out.println("登录成功");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static String getCode() {
    String valCode = "dny4";
    String path = "d://verifycode.jpg";
    Scanner sca=new Scanner(System.in);
    valCode=sca.next();
    return valCode;
}

}

输出结果:

当输入错误密码时会提示密码错误,请问为什么输入正确时就提示如图信息 信息: Redirect requested but followRedirects is disabled

2个回答

devmiao
devmiao   Ds   Rxr 2017.09.17 22:52
zy841958835
zy841958835   Ds   Rxr 2017.09.18 08:58

登录成功了么 调用下登录之后的页面是否有鉴权问题
2.用下httpclint最新的方法吧 http://blog.csdn.net/zy841958835/article/details/77992435

Csdn user default icon
上传中...
上传图片
插入图片