日期:2014-05-20  浏览次数:21099 次

谁玩过httpclient》为什么这段简单的代码验证码是空白的!
public class GetTest {

public static void main(String[] args) throws ClientProtocolException,
IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost proxy = new HttpHost("1.1.1.1",8080);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 
login(httpClient);
httpClient.getConnectionManager().shutdown();


}

public static void login(DefaultHttpClient httpclient)
throws ClientProtocolException, IOException {
HttpGet httpget = new HttpGet("https://captcha.yhd.com/public/validcode.do?t=" + new Random().nextInt(1000));
HttpResponse response = httpclient.execute(httpget);
File file = new File("login.jpg");
OutputStream ops = new FileOutputStream(file);
response.getEntity().writeTo(ops);
ops.close();
JFrame frame = new JFrame();
frame.setVisible(false);
frame.setBounds(100, 100, 100, 100);
frame.setLayout(new FlowLayout());
ImageIcon icon = new ImageIcon("login.jpg");
frame.add(new JLabel(icon));
frame.setVisible(true);
System.out.println("输入你看到的验证码");
Scanner scr = new Scanner(System.in);
String code = scr.nextLine();
EntityUtils.consume(response.getEntity());
HttpPost post = new HttpPost(
"https://passport.yhd.com/passport/login_input.do?");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "XXXXXXXX"));
nvps.add(new BasicNameValuePair("password", "XXXXXXXX"));
nvps.add(new BasicNameValuePair("validCode", code));
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(post);
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
}
}


公司内网,IE浏览网页是需要设置代理服务器,是不是还需要输入我的ID和用户名密码?
------解决方案--------------------
baidu 没问题。 
String urlString = "http://www.baidu.com/img/bdlogo.gif";
        URL url = new URL(urlString);  
        URLConnection con = url.openConnection();  
        con.setConnectTimeout(5*1000);  
        InputStream is = con.getInputStream();  
      
        byte[] bs = new byte[1024];  
        int len;  
        OutputStream os = new FileOutputStream("D:/bdlogo.gif");  
        while ((len = is.read(bs)) != -1) {  
          os.write(bs, 0, len);  
        }  
        os.close();  
        is.close();