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 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题