我要利用commons-httplcient 4.3 抓紧 :https://investorservice.cfmmc.com/veriCode.do ,这是个图片.我的抓取程序是:
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyStore;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;public class TestOCR { /**
* @param args
*/
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("E:\\jdk1.6.0_18\\jre\\lib\\security\\my.truststore")); //my.keystore
try {
trustStore.load(instream, "123456".toCharArray());
} finally {
try { instream.close(); } catch (Exception ignore) {}
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", 443, socketFactory);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet("https://investorservice.cfmmc.com/veriCode.do");
System.out.println("executing request " + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getEntity().getContentLength());
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
InputStream is=entity.getContent();
System.out.println("is length: " + is.available());
}
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}}
出错原因:
System.out.println(response.getEntity().getContentLength());输出的结果是-1
这是什么原因,有没有感兴趣的兄弟,试试看,https的ssl证书,你可以在:
http://download.csdn.net/detail/pdw2009/4075626
下载,然后导入jdk中。分不够可以再加,有兴趣的兄弟可以研究一下。
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyStore;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;public class TestOCR { /**
* @param args
*/
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("E:\\jdk1.6.0_18\\jre\\lib\\security\\my.truststore")); //my.keystore
try {
trustStore.load(instream, "123456".toCharArray());
} finally {
try { instream.close(); } catch (Exception ignore) {}
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", 443, socketFactory);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet("https://investorservice.cfmmc.com/veriCode.do");
System.out.println("executing request " + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getEntity().getContentLength());
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
InputStream is=entity.getContent();
System.out.println("is length: " + is.available());
}
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}}
出错原因:
System.out.println(response.getEntity().getContentLength());输出的结果是-1
这是什么原因,有没有感兴趣的兄弟,试试看,https的ssl证书,你可以在:
http://download.csdn.net/detail/pdw2009/4075626
下载,然后导入jdk中。分不够可以再加,有兴趣的兄弟可以研究一下。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货