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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘