Get请求
/**
* Get请求
* @param url 请求地址
* @param encoding 编码
* @param properties 请求头
* @return
*/
public static String sendGet(String url,String encoding,Map<String,String> properties){
URL realUrl=null;
BufferedReader br=null;
HttpURLConnection conn=null;
InputStream in=null;
StringBuffer sb=null;
try {
realUrl = new URL(url);
int y=1;
while(true){
try{
conn=(HttpURLConnection)realUrl.openConnection();
conn.setRequestMethod("GET");
for (String key : properties.keySet()) {
conn.setRequestProperty(key,properties.get(key));
}
conn.connect();
break;
}catch(IOException e){
Thread.sleep(60000);
e.printStackTrace();
continue;
}finally{
if(y>=3) break;
y++;
}
}
Map<String, List<String>> map = conn.getHeaderFields();
in=conn.getInputStream();
if(map.containsKey("Content-Encoding")&&map.get("Content-Encoding").contains("gzip")){
GZIPInputStream gzin = new GZIPInputStream(in);
InputStreamReader isr = new InputStreamReader(gzin,encoding);
// 定义BufferedReader输入流来读取URL的响应
br = new BufferedReader(isr);
}else{
br = new BufferedReader(new InputStreamReader(in,encoding));
}
int i=1;
while(true){
String page=null;
sb=new StringBuffer();
try{
//System.out.println(i);
while((page=br.readLine())!=null){
sb.append(page+"\n");
}
break;
}catch(IOException e){
Thread.sleep(60000);
e.printStackTrace();
continue;
}finally{
if(i>3){
break;
}
i++;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(in!=null) in.close();
if(br!=null) br.close();;
if(conn!=null) conn=null;
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Post请求
/**
* Post请求
* @param url 请求地址
* @param param 参数
* @param encoding 编码
* @param properties 请求头
* @return
*/
public static String sendPost(String url,String param,String encoding,Map<String,String> properties){
PrintWriter out = null;
BufferedReader in = null;
StringBuffer sb = null;
try {
URL realUrl = new URL(url);
int y=1;
URLConnection conn =null;
while(true){
try{
Thread.sleep(2000);
conn = realUrl.openConnection();
for (String key : properties.keySet()) {
conn.setRequestProperty(key,properties.get(key));
}
conn.setRequestProperty("Charset", encoding);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
break;
}catch(Exception e){
e.printStackTrace();
continue;
}finally{
if(y>=3) break;
y++;
}
}
Map<String, List<String>> map = conn.getHeaderFields();
InputStream is=conn.getInputStream();
if(map.containsKey("Content-Encoding")&&map.get("Content-Encoding").contains("gzip")){
GZIPInputStream gzin = new GZIPInputStream(is);
InputStreamReader isr = new InputStreamReader(gzin,encoding);
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(isr);
}else{
in = new BufferedReader(new InputStreamReader(is,encoding));
}
String line;
sb=new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return sb.toString();
}
代理设置,openConnection之前设置
Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress("ip", "端口号"));
conn = realUrl.openConnection(proxy);