import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException; public class Movision_verifyImage { private static HttpClient hc = new DefaultHttpClient(); public static void main(String args[]) throws ClientProtocolException, IOException, ParseException, URISyntaxException{ //获取图片验证码页面随机参数(当前时间) long date = new Date().getTime(); System.out.println(date); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("random",Long.toString(date))); get("http://172.16.3.6/admin/portalVerifyImage",params); } /* * 带参数的GET请求 * */ public static void get(String url,List<NameValuePair> params) throws ParseException, UnsupportedEncodingException, IOException, URISyntaxException{ //get请求 HttpGet httpget = new HttpGet(url); //设置参数 String str = EntityUtils.toString(new UrlEncodedFormEntity(params)); httpget.setURI(new URI(httpget.getURI().toString()+"?"+str)); //发送请求 HttpResponse re = hc.execute(httpget); //获取相应实体 HttpEntity entity = re.getEntity(); if (entity != null && entity.isStreaming()) { File storeFile = new File("F:\\test.jpg"); FileOutputStream fos = new FileOutputStream(storeFile); // 将取得的文件文件流写入目标文件 InputStream is = entity.getContent(); byte[] b = new byte[1024]; int j = 0; while ((j = is.read(b)) != -1) { fos.write(b, 0, j); } fos.flush(); fos.close(); } else { System.out.println("[" + url + "] 未找到."); return; } //关闭连接 hc.getConnectionManager().shutdown(); } } |