自带的easyclick访问https网页时,会有ssl timeout的警告
所以就自己弄个原生的。
importClass(java.lang.StringBuilder);
importClass(java.text.SimpleDateFormat);
importClass(java.io.BufferedReader);
importClass(java.io.InputStream);
importClass(java.io.InputStreamReader);
importClass(java.net.HttpURLConnection);
importClass(java.net.URL);
let nhttpClient = (function () {
function nhttpClient() {
}
nhttpClient.prototype.httpGetDefault = function (address) {
return httpGetDefault(address);
}
function httpGetDefault(address){
let content = "";
let conn = null; //HttpURLConnection
try{
let url = new URL(address);
conn = url.openConnection();
conn.setRequestMethod("GET");
//int code = conn.getResponseCode();
//conn.setRequestMethod("GET");
conn.setConnectTimeout(20000);
conn.setReadTimeout(20000);
let ins = conn.getInputStream(); //InputStream
let reader = new BufferedReader(new InputStreamReader(ins)); //BufferedReader
let response = new StringBuilder(); //StringBuilder
let line;
while((line = reader.readLine()) != null){
response.append(line);
}
content=response.toString();
}catch (e){
logd(e);
} finally{
if(conn != null){
conn.disconnect();
}
}
return content;
}
return nhttpClient;
})();
//使用实例
var nHttp = new nhttpClient();
var result = nHttp.httpGetDefault("https://www.baidu.com");
需要java版的,看下面示例:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
public class HttpUrlConnectionUtils {
private static final String TAG = "HttpUrlConnectionUtils";
private static final String DEF_CHATSET = "UTF-8";
private static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
private static int DEF_CONN_TIMEOUT = 20000;
public static String httpGetDefault(String address){
String content = "";
HttpURLConnection conn = null;
try{
URL url = new URL(address);
conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
//int code = conn.getResponseCode();
//conn.setRequestMethod("GET");
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_CONN_TIMEOUT);
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while( (line = reader.readLine()) != null ){
response.append(line);
}
content=response.toString();
}catch (Exception e){
e.printStackTrace();
} finally{
if(conn != null){
conn.disconnect();
}
}
return content;
}
public static String doGET(String strUrl, Map params) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
StringBuffer sb = new StringBuffer();
strUrl = strUrl + "?" + urlencode(params);
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}
public static String doPOST(String strUrl, Map params) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
StringBuffer sb = new StringBuffer();
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(urlencode(params));
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}
// 将map型转为请求参数型
private static String urlencode(Map<String, Object> params) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
for (Map.Entry i : params.entrySet()) {
try {
sb.append(i.getKey()).append("=")
.append(URLEncoder.encode(i.getValue() + "", "UTF-8"))
.append("&");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
String res=sb.toString();
res = res.substring(0,res.length()- 1);
return res;
}
}
笔记写完。