- 浏览: 37928 次
- 性别:
- 来自: 深圳
文章分类
最新评论
package org.zlex.commons.net;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Properties;
/**
* 网络工具
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public abstract class NetUtils {
public static final String CHARACTER_ENCODING = "UTF-8";
public static final String PATH_SIGN = "/";
public static final String METHOD_POST = "POST";
public static final String METHOD_GET = "GET";
public static final String CONTENT_TYPE = "Content-Type";
/**
* 以POST方式向指定地址发送数据包请求,并取得返回的数据包
*
* @param urlString
* @param requestData
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPost(String urlString, byte[] requestData)
throws Exception {
Properties requestProperties = new Properties();
requestProperties.setProperty(CONTENT_TYPE,
"application/octet-stream; charset=utf-8");
return requestPost(urlString, requestData, requestProperties);
}
/**
* 以POST方式向指定地址发送数据包请求,并取得返回的数据包
*
* @param urlString
* @param requestData
* @param requestProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPost(String urlString, byte[] requestData,
Properties requestProperties) throws Exception {
byte[] responseData = null;
HttpURLConnection con = null;
try {
URL url = new URL(urlString);
con = (HttpURLConnection) url.openConnection();
if ((requestProperties != null) && (requestProperties.size() > 0)) {
for (Map.Entry<Object, Object> entry : requestProperties
.entrySet()) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
con.setRequestProperty(key, value);
}
}
con.setRequestMethod(METHOD_POST); // 置为POST方法
con.setDoInput(true); // 开启输入流
con.setDoOutput(true); // 开启输出流
// 如果请求数据不为空,输出该数据。
if (requestData != null) {
DataOutputStream dos = new DataOutputStream(con
.getOutputStream());
dos.write(requestData);
dos.flush();
dos.close();
}
int length = con.getContentLength();
// 如果回复消息长度不为-1,读取该消息。
if (length != -1) {
DataInputStream dis = new DataInputStream(con.getInputStream());
responseData = new byte[length];
dis.readFully(responseData);
dis.close();
}
} catch (Exception e) {
throw e;
} finally {
if (con != null) {
con.disconnect();
con = null;
}
}
return responseData;
}
/**
* 以POST方式向指定地址提交表单<br>
* arg0=urlencode(value0)&arg1=urlencode(value1)
*
* @param urlString
* @param formProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPostForm(String urlString,
Properties formProperties) throws Exception {
return requestPostForm(urlString, formProperties, null);
}
/**
* 以POST方式向指定地址提交表单<br>
* arg0=urlencode(value0)&arg1=urlencode(value1)
*
* @param urlString
* @param formProperties
* @param requestProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPostForm(String urlString,
Properties formProperties, Properties requestProperties)
throws Exception {
requestProperties.setProperty(HttpUtils.CONTENT_TYPE,
"application/x-www-form-urlencoded");
StringBuilder sb = new StringBuilder();
if ((formProperties != null) && (formProperties.size() > 0)) {
for (Map.Entry<Object, Object> entry : formProperties.entrySet()) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
sb.append(key);
sb.append("=");
sb.append(encode(value));
sb.append("&");
}
}
String str = sb.toString();
str = str.substring(0, (str.length() - 1)); // 截掉末尾字符&
return requestPost(urlString, str.getBytes(CHARACTER_ENCODING),
requestProperties);
}
/**
* url解码
*
* @param str
* @return 解码后的字符串,当异常时返回原始字符串。
*/
public static String decode(String url) {
try {
return URLDecoder.decode(url, CHARACTER_ENCODING);
} catch (UnsupportedEncodingException ex) {
return url;
}
}
/**
* url编码
*
* @param str
* @return 编码后的字符串,当异常时返回原始字符串。
*/
public static String encode(String url) {
try {
return URLEncoder.encode(url, CHARACTER_ENCODING);
} catch (UnsupportedEncodingException ex) {
return url;
}
}
}
/**
* 2008-12-26
*/
package org.zlex.commons.net;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Properties;
/**
* 网络工具
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public abstract class NetUtils {
public static final String CHARACTER_ENCODING = "UTF-8";
public static final String PATH_SIGN = "/";
public static final String METHOD_POST = "POST";
public static final String METHOD_GET = "GET";
public static final String CONTENT_TYPE = "Content-Type";
/**
* 以POST方式向指定地址发送数据包请求,并取得返回的数据包
*
* @param urlString
* @param requestData
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPost(String urlString, byte[] requestData)
throws Exception {
Properties requestProperties = new Properties();
requestProperties.setProperty(CONTENT_TYPE,
"application/octet-stream; charset=utf-8");
return requestPost(urlString, requestData, requestProperties);
}
/**
* 以POST方式向指定地址发送数据包请求,并取得返回的数据包
*
* @param urlString
* @param requestData
* @param requestProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPost(String urlString, byte[] requestData,
Properties requestProperties) throws Exception {
byte[] responseData = null;
HttpURLConnection con = null;
try {
URL url = new URL(urlString);
con = (HttpURLConnection) url.openConnection();
if ((requestProperties != null) && (requestProperties.size() > 0)) {
for (Map.Entry<Object, Object> entry : requestProperties
.entrySet()) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
con.setRequestProperty(key, value);
}
}
con.setRequestMethod(METHOD_POST); // 置为POST方法
con.setDoInput(true); // 开启输入流
con.setDoOutput(true); // 开启输出流
// 如果请求数据不为空,输出该数据。
if (requestData != null) {
DataOutputStream dos = new DataOutputStream(con
.getOutputStream());
dos.write(requestData);
dos.flush();
dos.close();
}
int length = con.getContentLength();
// 如果回复消息长度不为-1,读取该消息。
if (length != -1) {
DataInputStream dis = new DataInputStream(con.getInputStream());
responseData = new byte[length];
dis.readFully(responseData);
dis.close();
}
} catch (Exception e) {
throw e;
} finally {
if (con != null) {
con.disconnect();
con = null;
}
}
return responseData;
}
/**
* 以POST方式向指定地址提交表单<br>
* arg0=urlencode(value0)&arg1=urlencode(value1)
*
* @param urlString
* @param formProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPostForm(String urlString,
Properties formProperties) throws Exception {
return requestPostForm(urlString, formProperties, null);
}
/**
* 以POST方式向指定地址提交表单<br>
* arg0=urlencode(value0)&arg1=urlencode(value1)
*
* @param urlString
* @param formProperties
* @param requestProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPostForm(String urlString,
Properties formProperties, Properties requestProperties)
throws Exception {
requestProperties.setProperty(HttpUtils.CONTENT_TYPE,
"application/x-www-form-urlencoded");
StringBuilder sb = new StringBuilder();
if ((formProperties != null) && (formProperties.size() > 0)) {
for (Map.Entry<Object, Object> entry : formProperties.entrySet()) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
sb.append(key);
sb.append("=");
sb.append(encode(value));
sb.append("&");
}
}
String str = sb.toString();
str = str.substring(0, (str.length() - 1)); // 截掉末尾字符&
return requestPost(urlString, str.getBytes(CHARACTER_ENCODING),
requestProperties);
}
/**
* url解码
*
* @param str
* @return 解码后的字符串,当异常时返回原始字符串。
*/
public static String decode(String url) {
try {
return URLDecoder.decode(url, CHARACTER_ENCODING);
} catch (UnsupportedEncodingException ex) {
return url;
}
}
/**
* url编码
*
* @param str
* @return 编码后的字符串,当异常时返回原始字符串。
*/
public static String encode(String url) {
try {
return URLEncoder.encode(url, CHARACTER_ENCODING);
} catch (UnsupportedEncodingException ex) {
return url;
}
}
}
注意上述requestPostForm()方法,是用来提交表单的。
测试用例
Java代码
/**
* 2009-8-21
*/
package org.zlex.commons.net;
import static org.junit.Assert.*;
import java.util.Properties;
import org.junit.Test;
/**
* 网络工具测试
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public class NetUtilsTest {
/**
* Test method for
* {@link org.zlex.commons.net.NetUtils#requestPost(java.lang.String, byte[])}
* .
*/
@Test
public final void testRequestPostStringByteArray() throws Exception {
Properties requestProperties = new Properties();
// 模拟浏览器信息
requestProperties
.put(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; TencentTraveler ; .NET CLR 1.1.4322)");
byte[] b = NetUtils.requestPost("http://localhost:8080/zlex/post.do",
"XML".getBytes());
System.err.println(new String(b, "utf-8"));
}
/**
* Test method for
* {@link org.zlex.commons.net.NetUtils#requestPostForm(java.lang.String, java.util.Properties)}
* .
*/
@Test
public final void testRequestPostForm() throws Exception {
Properties formProperties = new Properties();
formProperties.put("j_username", "Admin");
formProperties.put("j_password", "manage");
byte[] b = NetUtils.requestPostForm(
"http://localhost:8080/zlex/j_spring_security_check",
formProperties);
System.err.println(new String(b, "utf-8"));
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Properties;
/**
* 网络工具
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public abstract class NetUtils {
public static final String CHARACTER_ENCODING = "UTF-8";
public static final String PATH_SIGN = "/";
public static final String METHOD_POST = "POST";
public static final String METHOD_GET = "GET";
public static final String CONTENT_TYPE = "Content-Type";
/**
* 以POST方式向指定地址发送数据包请求,并取得返回的数据包
*
* @param urlString
* @param requestData
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPost(String urlString, byte[] requestData)
throws Exception {
Properties requestProperties = new Properties();
requestProperties.setProperty(CONTENT_TYPE,
"application/octet-stream; charset=utf-8");
return requestPost(urlString, requestData, requestProperties);
}
/**
* 以POST方式向指定地址发送数据包请求,并取得返回的数据包
*
* @param urlString
* @param requestData
* @param requestProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPost(String urlString, byte[] requestData,
Properties requestProperties) throws Exception {
byte[] responseData = null;
HttpURLConnection con = null;
try {
URL url = new URL(urlString);
con = (HttpURLConnection) url.openConnection();
if ((requestProperties != null) && (requestProperties.size() > 0)) {
for (Map.Entry<Object, Object> entry : requestProperties
.entrySet()) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
con.setRequestProperty(key, value);
}
}
con.setRequestMethod(METHOD_POST); // 置为POST方法
con.setDoInput(true); // 开启输入流
con.setDoOutput(true); // 开启输出流
// 如果请求数据不为空,输出该数据。
if (requestData != null) {
DataOutputStream dos = new DataOutputStream(con
.getOutputStream());
dos.write(requestData);
dos.flush();
dos.close();
}
int length = con.getContentLength();
// 如果回复消息长度不为-1,读取该消息。
if (length != -1) {
DataInputStream dis = new DataInputStream(con.getInputStream());
responseData = new byte[length];
dis.readFully(responseData);
dis.close();
}
} catch (Exception e) {
throw e;
} finally {
if (con != null) {
con.disconnect();
con = null;
}
}
return responseData;
}
/**
* 以POST方式向指定地址提交表单<br>
* arg0=urlencode(value0)&arg1=urlencode(value1)
*
* @param urlString
* @param formProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPostForm(String urlString,
Properties formProperties) throws Exception {
return requestPostForm(urlString, formProperties, null);
}
/**
* 以POST方式向指定地址提交表单<br>
* arg0=urlencode(value0)&arg1=urlencode(value1)
*
* @param urlString
* @param formProperties
* @param requestProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPostForm(String urlString,
Properties formProperties, Properties requestProperties)
throws Exception {
requestProperties.setProperty(HttpUtils.CONTENT_TYPE,
"application/x-www-form-urlencoded");
StringBuilder sb = new StringBuilder();
if ((formProperties != null) && (formProperties.size() > 0)) {
for (Map.Entry<Object, Object> entry : formProperties.entrySet()) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
sb.append(key);
sb.append("=");
sb.append(encode(value));
sb.append("&");
}
}
String str = sb.toString();
str = str.substring(0, (str.length() - 1)); // 截掉末尾字符&
return requestPost(urlString, str.getBytes(CHARACTER_ENCODING),
requestProperties);
}
/**
* url解码
*
* @param str
* @return 解码后的字符串,当异常时返回原始字符串。
*/
public static String decode(String url) {
try {
return URLDecoder.decode(url, CHARACTER_ENCODING);
} catch (UnsupportedEncodingException ex) {
return url;
}
}
/**
* url编码
*
* @param str
* @return 编码后的字符串,当异常时返回原始字符串。
*/
public static String encode(String url) {
try {
return URLEncoder.encode(url, CHARACTER_ENCODING);
} catch (UnsupportedEncodingException ex) {
return url;
}
}
}
/**
* 2008-12-26
*/
package org.zlex.commons.net;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Properties;
/**
* 网络工具
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public abstract class NetUtils {
public static final String CHARACTER_ENCODING = "UTF-8";
public static final String PATH_SIGN = "/";
public static final String METHOD_POST = "POST";
public static final String METHOD_GET = "GET";
public static final String CONTENT_TYPE = "Content-Type";
/**
* 以POST方式向指定地址发送数据包请求,并取得返回的数据包
*
* @param urlString
* @param requestData
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPost(String urlString, byte[] requestData)
throws Exception {
Properties requestProperties = new Properties();
requestProperties.setProperty(CONTENT_TYPE,
"application/octet-stream; charset=utf-8");
return requestPost(urlString, requestData, requestProperties);
}
/**
* 以POST方式向指定地址发送数据包请求,并取得返回的数据包
*
* @param urlString
* @param requestData
* @param requestProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPost(String urlString, byte[] requestData,
Properties requestProperties) throws Exception {
byte[] responseData = null;
HttpURLConnection con = null;
try {
URL url = new URL(urlString);
con = (HttpURLConnection) url.openConnection();
if ((requestProperties != null) && (requestProperties.size() > 0)) {
for (Map.Entry<Object, Object> entry : requestProperties
.entrySet()) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
con.setRequestProperty(key, value);
}
}
con.setRequestMethod(METHOD_POST); // 置为POST方法
con.setDoInput(true); // 开启输入流
con.setDoOutput(true); // 开启输出流
// 如果请求数据不为空,输出该数据。
if (requestData != null) {
DataOutputStream dos = new DataOutputStream(con
.getOutputStream());
dos.write(requestData);
dos.flush();
dos.close();
}
int length = con.getContentLength();
// 如果回复消息长度不为-1,读取该消息。
if (length != -1) {
DataInputStream dis = new DataInputStream(con.getInputStream());
responseData = new byte[length];
dis.readFully(responseData);
dis.close();
}
} catch (Exception e) {
throw e;
} finally {
if (con != null) {
con.disconnect();
con = null;
}
}
return responseData;
}
/**
* 以POST方式向指定地址提交表单<br>
* arg0=urlencode(value0)&arg1=urlencode(value1)
*
* @param urlString
* @param formProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPostForm(String urlString,
Properties formProperties) throws Exception {
return requestPostForm(urlString, formProperties, null);
}
/**
* 以POST方式向指定地址提交表单<br>
* arg0=urlencode(value0)&arg1=urlencode(value1)
*
* @param urlString
* @param formProperties
* @param requestProperties
* @return 返回数据包
* @throws Exception
*/
public static byte[] requestPostForm(String urlString,
Properties formProperties, Properties requestProperties)
throws Exception {
requestProperties.setProperty(HttpUtils.CONTENT_TYPE,
"application/x-www-form-urlencoded");
StringBuilder sb = new StringBuilder();
if ((formProperties != null) && (formProperties.size() > 0)) {
for (Map.Entry<Object, Object> entry : formProperties.entrySet()) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
sb.append(key);
sb.append("=");
sb.append(encode(value));
sb.append("&");
}
}
String str = sb.toString();
str = str.substring(0, (str.length() - 1)); // 截掉末尾字符&
return requestPost(urlString, str.getBytes(CHARACTER_ENCODING),
requestProperties);
}
/**
* url解码
*
* @param str
* @return 解码后的字符串,当异常时返回原始字符串。
*/
public static String decode(String url) {
try {
return URLDecoder.decode(url, CHARACTER_ENCODING);
} catch (UnsupportedEncodingException ex) {
return url;
}
}
/**
* url编码
*
* @param str
* @return 编码后的字符串,当异常时返回原始字符串。
*/
public static String encode(String url) {
try {
return URLEncoder.encode(url, CHARACTER_ENCODING);
} catch (UnsupportedEncodingException ex) {
return url;
}
}
}
注意上述requestPostForm()方法,是用来提交表单的。
测试用例
Java代码
/**
* 2009-8-21
*/
package org.zlex.commons.net;
import static org.junit.Assert.*;
import java.util.Properties;
import org.junit.Test;
/**
* 网络工具测试
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public class NetUtilsTest {
/**
* Test method for
* {@link org.zlex.commons.net.NetUtils#requestPost(java.lang.String, byte[])}
* .
*/
@Test
public final void testRequestPostStringByteArray() throws Exception {
Properties requestProperties = new Properties();
// 模拟浏览器信息
requestProperties
.put(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; TencentTraveler ; .NET CLR 1.1.4322)");
byte[] b = NetUtils.requestPost("http://localhost:8080/zlex/post.do",
"XML".getBytes());
System.err.println(new String(b, "utf-8"));
}
/**
* Test method for
* {@link org.zlex.commons.net.NetUtils#requestPostForm(java.lang.String, java.util.Properties)}
* .
*/
@Test
public final void testRequestPostForm() throws Exception {
Properties formProperties = new Properties();
formProperties.put("j_username", "Admin");
formProperties.put("j_password", "manage");
byte[] b = NetUtils.requestPostForm(
"http://localhost:8080/zlex/j_spring_security_check",
formProperties);
System.err.println(new String(b, "utf-8"));
}
}
发表评论
文章已被作者锁定,不允许评论。
相关推荐
Http请求模拟报文返回工具,使用配置模拟http响应报文,war包部署在tomcat中,启动tomcat即可访问url模拟响应
c# http请求模拟
restclient http请求模拟工具可以设置http请求头、body体 http接口测试工具
此文档是关于多线程客户端请求模拟, 要配合MultiServer服务器模拟程序使用. 客户端运行时,会生成10个子线程来请求服务端.
请求模拟 js本地运行
chrome浏览器插件,可模拟各种客户端请求
文件压缩解压、office文档读写、http,ftp请求模拟、shell执行、模板引擎
Postman 工具(mac 环境)
里面包含安装应用程序以及说明文档,还有使用更新说明,亲测可用
用HTML页面模拟测试get/post请求,psot请求参数的格式为json串
工具小巧
网络请求模拟器,可以自定义Header内容,以及多种格式参数的访问
用于模拟各种类型的网络请求,测试功能方便快捷,支持POST、GET请求,32位、64位均有
改资源中携带报文模拟范例,亲测有效。注释详细,步骤突出!源于真实需求,工具类拿来急用,具体业务可以稍调一下
亲测可用的模拟请求工具开发测试接口神器,绝对可用postman
httppost可以模拟post发送和get方法向目标网站提交请求,这个是电脑端的软件,不是运行于web的脚本代码 1. testhttppost.exe:http post上传文件工具; 2. TestHttpPost: 源码; 3.lr_post.txt: 录制的loadrunner ...
用户http模拟提交的一款小工具,免费使用,打卡即用,可以模拟http、https、cookie等提交get、post或自定义协议头请求,非常方便好用
模拟Http请求.
多种请求方式请求url得到回调,支持params(key,value)传值。 支持window,ios,Linux
一个简单的在线模拟HTTP请求模拟解决方案 基本网址 https://mok.now.sh/ 请求示例: wget -q -O - " $@ " " mok.now.sh/?response={ \" hello \" : \" world \" }&responseType=json " 路径 您可以使用任何喜欢的...