模拟请求 get 和 post 发送 xml\json\a=1&b=2&c= 等形式参数,代理设置
工具类-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
public class HttpURLConnectionUtils {
private String ServicePort_shezhi = "0";// 是否使用代理:0-NO,1-YES
private String proxyHost = "192.168.1.1";// 代理IP
private String proxyPort = "8080";// 代理端口
/**
* 仅仅在发送url的时候调用 get 方式请求
*
* @param url
* @return
*/
public String sendHttpURLConnectionGet(String url) {
String resultStr = "";
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL getUrl = new URL(url);
// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
// 代理判断,0-不使用代理;1-使用代理
if ("0".equals(ServicePort_shezhi)) {
connection = (HttpURLConnection) getUrl.openConnection();
} else {
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, Integer.valueOf(proxyPort)));
connection = (HttpURLConnection) getUrl.openConnection(proxy);
}
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
connection.setConnectTimeout(25000);
connection.setReadTimeout(25000);
connection.setRequestMethod("GET");
connection.connect();
// 取得输入流,并使用Reader读取
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String lines;
// 获取返回的字符流转化为字符串
while ((lines = reader.readLine()) != null) {
resultStr = resultStr + lines;
}
return resultStr;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("请求出现异常,返回的字符串为:" + resultStr + " e.getMessage()=" + e.getMessage());
} finally {
try {
if (reader != null) {
reader.close();
}
if (connection != null) {
// 断开连接
connection.disconnect();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 运行到此说明发生错误
resultStr = "errcode";
return resultStr;
}
/**
* post 方式请求 可以发送xml,json,a=1&b=2&c= 等格式的字符串
*
* @param remoteUrl
* @param xmlStr
* @param encoding
* utf-8
* @return
*/
public String sendHttpURLConnectionPost(String remoteUrl, String xmlStr, String encoding) {
String strResponse = "";
String strMessage = "";
InputStream inputStream = null;
OutputStream outputStream = null;
BufferedReader reader = null;
OutputStreamWriter writer = null;
URL url;
HttpURLConnection connection;
try {
url = new URL(remoteUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setAllowUserInteraction(true);
// 设置超时
String strTimeOut = "6000";
if (strTimeOut != null) {
connection.setConnectTimeout(Integer.parseInt(strTimeOut));
connection.setReadTimeout(Integer.parseInt(strTimeOut));
}
connection.connect();
outputStream = connection.getOutputStream();
writer = new OutputStreamWriter(outputStream, encoding);
writer.write(xmlStr);
writer.flush();
inputStream = connection.getInputStream();
int status = connection.getResponseCode();
if (status == 200) {
reader = new BufferedReader(new InputStreamReader(inputStream, encoding));
while ((strMessage = reader.readLine()) != null) {
strResponse += strMessage;
}
} else {
strResponse = "error http's status=" + status;
}
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (Exception e) {
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (reader != null) {
reader.close();
}
if (outputStream != null) {
outputStream.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("出现异常,异常信息为+" + e.getMessage());
}
}
return strResponse;
}
/**
* put 方式请求 可以发送xml,json,a=1&b=2&c= 等格式的字符串
*
* @param remoteUrl
* @param xmlStr
* @param encoding
* utf-8
* @return
*/
public String sendHttpURLConnectionPut(String remoteUrl, String xmlStr, String encoding) {
String strResponse = "";
String strMessage = "";
InputStream inputStream = null;
OutputStream outputStream = null;
BufferedReader reader = null;
OutputStreamWriter writer = null;
URL url;
HttpURLConnection connection;
try {
url = new URL(remoteUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setAllowUserInteraction(true);
// 设置超时
String strTimeOut = "6000";
if (strTimeOut != null) {
connection.setConnectTimeout(Integer.parseInt(strTimeOut));
connection.setReadTimeout(Integer.parseInt(strTimeOut));
}
connection.connect();
outputStream = connection.getOutputStream();
writer = new OutputStreamWriter(outputStream, encoding);
writer.write(xmlStr);
writer.flush();
inputStream = connection.getInputStream();
int status = connection.getResponseCode();
if (status == 200) {
reader = new BufferedReader(new InputStreamReader(inputStream, encoding));
while ((strMessage = reader.readLine()) != null) {
strResponse += strMessage;
}
} else {
strResponse = "error http's status=" + status;
}
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (Exception e) {
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (reader != null) {
reader.close();
}
if (outputStream != null) {
outputStream.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("出现异常,异常信息为+" + e.getMessage());
}
}
return strResponse;
}
/**
* delete 方式请求 可以发送xml,json,a=1&b=2&c= 等格式的字符串
*
* @param remoteUrl
* @param xmlStr
* @param encoding
* utf-8
* @return
*/
public String sendHttpURLConnectionDelete(String remoteUrl, String xmlStr, String encoding) {
String strResponse = "";
String strMessage = "";
InputStream inputStream = null;
OutputStream outputStream = null;
BufferedReader reader = null;
OutputStreamWriter writer = null;
URL url;
HttpURLConnection connection;
try {
url = new URL(remoteUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("DELETE");
connection.setAllowUserInteraction(true);
// 设置超时
String strTimeOut = "6000";
if (strTimeOut != null) {
connection.setConnectTimeout(Integer.parseInt(strTimeOut));
connection.setReadTimeout(Integer.parseInt(strTimeOut));
}
connection.connect();
outputStream = connection.getOutputStream();
writer = new OutputStreamWriter(outputStream, encoding);
writer.write(xmlStr);
writer.flush();
inputStream = connection.getInputStream();
int status = connection.getResponseCode();
if (status == 200) {
reader = new BufferedReader(new InputStreamReader(inputStream, encoding));
while ((strMessage = reader.readLine()) != null) {
strResponse += strMessage;
}
} else {
strResponse = "error http's status=" + status;
}
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (Exception e) {
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (reader != null) {
reader.close();
}
if (outputStream != null) {
outputStream.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("出现异常,异常信息为+" + e.getMessage());
}
}
return strResponse;
}
}
模拟调用-需要被调用的服务处于启动状态哟
//get 方式请求
@Test
public void testGetChannelOrderListGet(){
String url="http://localhost:8080/o/get/list?page=1&mo=2&ed=2&os=&ps=";
HttpURLConnectionUtils httpUtils=new HttpURLConnectionUtils();
String result=httpUtils.sendHttpURLConnectionGet(url);
System.out.println("resultJson:"+result);
}
//post 方式请求
@Test
public void testGetChannelOrderListPost(){
String url="http://localhost:8080/o/get/list?";
String xml="page=1&mo=2&ed=2&os=&ps=";
HttpURLConnectionUtils httpUtils=new HttpURLConnectionUtils();
String result=httpUtils.sendHttpURLConnectionPost(url,xml,"utf-8");
System.out.println("resultJson:"+result);
}
新增 put