Grooter 2017-09-17 11:25 采纳率: 100%
浏览 1590

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 2017-09-17 14:52
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog