httpclient4模拟登陆问题,用OCR可以识别验证码,但是模拟post的时候总是说验证码有误,求帮忙
import java.awt.image.BufferedImage; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.asprise.util.ocr.OCR; /** * @title: test2.java * @package * @description: TODO * @author caiyigo * @date 2012-9-15 下午9:17:59 * @version v1.00 */ public class test2 { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpGet httpget = new HttpGet( "http://query.5184.com/query/image.jsp"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("获取验证码: " + response.getStatusLine()); /* System.out.println(EntityUtils.toString(entity)); */ InputStream is = entity.getContent(); BufferedImage image = ImageIO.read(is); String imgcode = new OCR().recognizeEverything(image); System.out.println("n---- 验证码是: ------- n" + imgcode); HttpPost httpost = new HttpPost( "http://query.5184.com/query/zk/zk_seat_2012_07.jsp"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("name0", "010411201317")); nvps.add(new BasicNameValuePair("rand", imgcode)); nvps.add(new BasicNameValuePair("serChecked", "on")); httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); response = httpclient.execute(httpost); entity = response.getEntity(); System.out.println(EntityUtils.toString(entity)); System.out.println("返回: " + response.getStatusLine()); System.out.println(response.getProtocolVersion()); System.out.println(response.getStatusLine().getStatusCode()); System.out.println(response.getStatusLine().getReasonPhrase()); EntityUtils.consume(entity); } finally { httpclient.getConnectionManager().shutdown(); } } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该是cookie的问题,get和post使用的不是同一个cookie
验证码识别后有一个空格,问题就在这里
首先把首页获取了来,然后再进一步获取验证码,主要是为了初始化Cookie;最好是给获取验证码的HttpGet加上Referer参数