package com.hx.util;
|
|
import java.io.BufferedReader;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.io.OutputStream;
|
import java.io.OutputStreamWriter;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
public class RequestMethod {
|
|
/**
|
* 发送Post请求
|
* @param path 请求路径
|
* @param params 请求参数
|
* @param encoding 编码
|
* @return 服务器端内容
|
*/
|
public static String sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
|
StringBuilder data = new StringBuilder();
|
if(params!=null && !params.isEmpty()){
|
for(Map.Entry<String, String> entry : params.entrySet()){
|
data.append(entry.getKey()).append("=");
|
data.append(URLEncoder.encode(entry.getValue(), encoding));
|
data.append("&");
|
}
|
data.deleteCharAt(data.length() - 1);
|
}
|
byte[] entity = data.toString().getBytes();//生成实体数据
|
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
|
conn.setConnectTimeout(5000);
|
conn.setRequestMethod("POST");
|
conn.setDoOutput(true);//允许对外输出数据
|
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
|
OutputStream outStream = conn.getOutputStream();
|
outStream.write(entity);
|
if(conn.getResponseCode() == 200){
|
return StreamUtils.InputStreamTOString(conn.getInputStream()) ;
|
}
|
return null;
|
}
|
|
/**
|
* 发送Post请求
|
* @param path 请求路径
|
* @param content 发送内容
|
* @param encoding 编码
|
* @return 服务器端内容
|
*/
|
public static String sendPOSTRequest(String path, String content, String encoding){
|
//byte[] entity = content.toString().getBytes();//生成实体数据
|
try{
|
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
|
conn.setConnectTimeout(5000);
|
conn.setRequestMethod("POST");
|
conn.setDoOutput(true);//允许对外输出数据
|
conn.setDoInput(true);
|
conn.setUseCaches(false);
|
conn.setRequestProperty("Charset", encoding);
|
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
//conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
|
OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
|
outStream.write(content);
|
outStream.flush();
|
if(conn.getResponseCode() == 200){
|
return StreamUtils.InputStreamTOString(conn.getInputStream());
|
}
|
}catch(Exception e){
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 发送Post请求
|
* @param path 请求路径
|
* @param content 发送内容
|
* @param header 请求头参数设置
|
* @param contentType 请求类型,默认application/x-www-form-urlencoded
|
* @param encoding 编码,默认UTF-8
|
* @return 服务器端内容
|
*/
|
public static String sendPOSTRequest(String path, String content,Map<String,String> header,String contentType, String encoding){
|
//byte[] entity = content.toString().getBytes();//生成实体数据
|
try{
|
if(StringUtils.isNull(contentType)){
|
contentType = "application/x-www-form-urlencoded";
|
}
|
if(StringUtils.isNull(encoding)){
|
encoding = "UTF-8";
|
}
|
|
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
|
conn.setConnectTimeout(5000);
|
conn.setRequestMethod("POST");
|
conn.setDoOutput(true);//允许对外输出数据
|
conn.setDoInput(true);
|
conn.setUseCaches(false);
|
conn.setRequestProperty("Charset", encoding);
|
conn.setRequestProperty("Content-Type",contentType);
|
if(header != null){
|
for(String key:header.keySet()){//keySet获取map集合key的集合 然后在遍历key即可
|
conn.setRequestProperty(key,header.get(key));
|
}
|
}
|
//conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
|
OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
|
outStream.write(content);
|
outStream.flush();
|
if(conn.getResponseCode() == 200){
|
return StreamUtils.InputStreamTOString(conn.getInputStream());
|
}
|
}catch(Exception e){
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 发送Post请求
|
* @param path 请求路径
|
* @param entity 发送内容
|
* @param encoding 编码
|
* @return 服务器端内容
|
*/
|
public static String sendPOSTRequest(String path, byte[] entity, String encoding) throws Exception{
|
System.out.println("entity:" + entity.length);
|
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
|
conn.setConnectTimeout(5000);
|
conn.setRequestMethod("POST");
|
conn.setDoOutput(true);//允许对外输出数据
|
conn.setRequestProperty("Content-Type", "application/octet-stream");
|
conn.setRequestProperty("Accept", "application/octet-stream");
|
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
|
OutputStream outStream = conn.getOutputStream();
|
outStream.write(entity);
|
if(conn.getResponseCode() == 200){
|
return StreamUtils.InputStreamTOString(conn.getInputStream());
|
}
|
return null;
|
}
|
|
/**
|
* 发送GET请求
|
* @param path 请求路径
|
* @param params 请求参数
|
* @return 服务器端内容
|
*/
|
public static String sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
|
// http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90
|
StringBuilder url = new StringBuilder(path);
|
if(params!=null){
|
url.append("?");
|
for(Map.Entry<String, String> entry : params.entrySet()){
|
url.append(entry.getKey()).append("=");
|
url.append(URLEncoder.encode(entry.getValue(), ecoding));
|
url.append("&");
|
}
|
url.deleteCharAt(url.length() - 1);
|
}
|
HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
|
conn.setConnectTimeout(5000);
|
conn.setRequestMethod("GET");
|
if(conn.getResponseCode() == 200){
|
return StreamUtils.InputStreamTOString(conn.getInputStream()) ;
|
}
|
return null;
|
}
|
|
|
|
public static String msg(InputStream in) throws Exception{
|
BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
String line;
|
List<String> lines = new ArrayList<String>();
|
while((line = br.readLine())!=null){
|
lines.add(line);
|
}
|
if(lines.size()>0){
|
String f = lines.get(0);
|
if(f.indexOf("200")!=-1){
|
String e = lines.get(lines.size()-1);
|
return e;
|
}else{
|
return null;
|
}
|
}else{
|
return null;
|
}
|
}
|
}
|