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 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启