81,092
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.ssl.HttpsURLConnection;
//以上是导入的包,没用的你自己删除了吧
/*
* https
*/
public static String getHttpsHtml(String urlStr,String param,String header) throws IOException{
urlStr=param!=null&&!"".equals(param)?urlStr+"?"+param:urlStr;
URL url = new URL(null,urlStr,new sun.net.www.protocol.https.Handler());//重点在这里,需要使用带有URLStreamHandler参数的URL构造方法
javax.net.ssl.HttpsURLConnection httpConnection = (javax.net.ssl.HttpsURLConnection) url.openConnection();//由于我调用的是官方给微信API接口,所以采用HTTPS连接
// httpConnection.setRequestProperty("accept", "text/html");
// httpConnection.setRequestProperty("connection", "utf-8");
// httpConnection.setRequestProperty("content-type","text/html;charset=utf-8");
if(header!=null){
header=header.split("\\) ")[0];
}
httpConnection.setRequestProperty("User-Agent", header+")");
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream urlStream = httpConnection.getInputStream();
// byte[] getData = readInputStream(urlStream); //获得网站的二进制数据
//
// String data = new String(getData);
// int strat=data.indexOf("charset=");
// if(strat!=-1){
// String bm=data.substring(strat+8,data.indexOf("\"",strat) );
// String bm1=data.substring(strat+9,data.indexOf("\"",strat+9) );
// data=new String(getData,"".equals(bm)?bm1:bm);
// }
// System.out.println(data);
// return data;
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = urlStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toString();
}
return "";
}
//调用
String html=IdlSDK.getHttpsHtml("https://detail.m.tmall.com/item.htm", "spm=a220m.1000858.1000725.6.6WYs3h&id=27478848618&areaId=320500&cat_id=50025784&rn=36535b7f9c8501df579a59e7fedf719a&user_id=675822543&is_b=1", "");