package mas.util;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;
import javax.activation.MimetypesFileTypeMap;
public class HttpConnection {
/**
* 向指定URL发送GET方法的请求
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。
* @return URL所代表远程资源的响应
*/
public String get(String url, String param) throws Exception {
String urlName = url + "?" + param;
return get(urlName);
}
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @return URL所代表远程资源的响应
*/
public String get(String url) throws Exception {
String result = "";
BufferedReader in = null;
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
conn.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
// 建立实际的连接
conn.connect();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
in.close();
return result;
}
/**
* 向指定URL发送POST方法的请求
* @param url 发送请求的URL
* @param content 内容
* @return URL所代表远程资源的响应
* @throws Exception
*/
public String post(String url,String content) throws Exception{
String result = "";
URL postUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
// out.writeBytes(content);
out.write(content.getBytes("UTF-8"));
out.flush();
out.close(); // flush and close
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码
String line="";
while ((line = reader.readLine()) != null){
result += line;
}
reader.close();
connection.disconnect();
return result;
}
/**
* 向指定URL发送POST方法的请求
* @Title: post
* @Description: TODO
* @param @param url
* @param @param textMap
* @param @return
* @return String
* @throws
*/
public String post(String url, Map<String, String> textMap){
String res = "";
HttpURLConnection conn = null;
String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符
try {
URL postUrl = new URL(url);
conn = (HttpURLConnection) postUrl.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (textMap != null) {
StringBuffer strBuf = new StringBuffer();
Iterator iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
strBuf.append("\r\n").append("--").append(BOUNDARY).append(
"\r\n");
strBuf.append("Content-Disposition: form-data; name=\""
+ inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
}
out.write(strBuf.toString().getBytes());
}
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();
// 读取返回数据
StringBuffer strBuf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
strBuf.append(line).append("\n");
}
res = strBuf.toString();
reader.close();
reader = null;
} catch (Exception e) {
System.out.println("发送POST请求出错。" + url);
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
return res;
}
/**
* 向指定URL发送POST方法的请求 (带文件)
* @param url 发送请求的URL
* @param textMap 文本参数键值
* @param fileMap 文件键值
* @return URL所代表远程资源的响应
* @throws Exception
*/
public String filePost(String url, Map<String, String> textMap,
Map<String, String> fileMap) {
String res = "";
HttpURLConnection conn = null;
String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符
try {
URL postUrl = new URL(url);
conn = (HttpURLConnection) postUrl.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (textMap != null) {
StringBuffer strBuf = new StringBuffer();
Iterator iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
strBuf.append("\r\n").append("--").append(BOUNDARY).append(
"\r\n");
strBuf.append("Content-Disposition: form-data; name=\""
+ inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
}
out.write(strBuf.toString().getBytes());
}
// file
if (fileMap != null) {
Iterator iter = fileMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
File file = new File(inputValue);
String filename = f
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
mas.zip (61个子文件)
mas
.project 1KB
src
test
java
main
webapp
WEB-INF
web.xml 708B
jquery-3.1.0.min.js 84KB
index.jsp 2KB
resources
config.properties 89B
java
mas
util
SSLClient.java 2KB
HttpConnection.java 9KB
JsAPIConfig.java 2KB
SHA1.java 954B
consts
WxConfig.java 836B
MsgType.java 1KB
XmlResp.java 2KB
servlet
BackServlet.java 2KB
api
DeviceApi.java 4KB
json
DeviceAuth.java 4KB
AccessToken.java 1KB
WebApi.java 2KB
MpApi.java 3KB
util
AccessTokenUtil.java 3KB
HttpUtil.java 4KB
tool
Tools.java 7KB
MatrixToImageWriter.java 1KB
target
tmp
jsp
maven-status
maven-compiler-plugin
testCompile
default-testCompile
inputFiles.lst 0B
compile
default-compile
createdFiles.lst 707B
inputFiles.lst 1KB
generated-sources
annotations
classes
config.properties 107B
mas
util
SSLClient$1.class 1015B
SSLClient.class 2KB
JsAPIConfig.class 2KB
SHA1.class 2KB
HttpConnection.class 8KB
consts
XmlResp.class 2KB
WxConfig.class 2KB
MsgType$Event.class 579B
MsgType$DeviceEvent.class 468B
MsgType.class 888B
servlet
BackServlet.class 3KB
api
WebApi.class 2KB
MpApi.class 2KB
json
DeviceAuth.class 2KB
AccessToken.class 1KB
util
AccessTokenUtil$1.class 734B
AccessTokenUtil$3.class 693B
HttpUtil.class 3KB
AccessTokenUtil$2.class 858B
HttpUtil$Utf8ResponseHandler.class 2KB
AccessTokenUtil.class 3KB
DeviceApi.class 4KB
tool
MatrixToImageWriter.class 2KB
Tools.class 5KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 736B
org.eclipse.wst.validation.prefs 50B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 57B
org.eclipse.wst.common.component 734B
.jsdtscope 575B
pom.xml 6KB
.classpath 1KB
共 61 条
- 1
赤色天空
- 粉丝: 9
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 大数据,字典大数据,字典大数据,字典大数据,字典大数据,字典
- 无标题123111113332
- 使用Hyperledger Fabric进行caliper压测,测试吞吐量及时延
- python-pandas-2.0.3及其依赖包+sqlalchemy-2.0.30及其依赖包+pymysql-1.1.0
- 通过IPIM实现命令行开关机
- Cat.1 AT指令和专业术语汇总
- “吃什么”软件,EW,eat what?
- unity中流血插件Real Blood v1.11
- 基于SpringBoot研发框架,它在SpringBoot的基础上,提供了诸如ReadinessCheck,类隔离,日志等能力
- 2024数维杯要点和难点,具体案例
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论5