JSD-8146 开源任务材料
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.5 KiB

package com.fr.plugin.sso.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* http请求工具类为支持jdk1.4,不使用泛型
*
* @author
*/
public class HttpRequest {
/**
* 向指定 URL 发送POST方法的请求
*
* @param fullurl 完整的URL可能包括一些参数如 ?a=B&c=D
* @return 所代表远程资源的响应结果
*/
public static String sendGet(String fullurl) {
StringBuffer result = new StringBuffer();
BufferedReader in = null;
try {
String urlNameString = fullurl;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
//connection.setRequestProperty("accept", "*/*");
//connection.setRequestProperty("connection", "Keep-Alive");
//connection.setRequestProperty("user-agent",
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// connection.setRequestProperty("Authorization", "Basic "+base64(user, password));
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map map = connection.getHeaderFields();
// 遍历所有的响应头字段
//for (String key : map.keySet()) {
// System.out.println(key + "--->" + map.get(key));
//}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
System.err.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result.toString();
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param params 请求参数
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, HashMap params) {
PrintWriter out = null;
BufferedReader in = null;
StringBuffer result = new StringBuffer();
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
// connection.setRequestProperty("accept", "*/*");
// connection.setRequestProperty("connection", "Keep-Alive");
// connection.setRequestProperty("user-agent",
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// connection.setRequestProperty("Authorization", "Basic "+base64(user, password));
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("contentType", "utf-8");
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(connection.getOutputStream());
// 发送请求参数
StringBuffer param = new StringBuffer();
for (Iterator iterator = params.keySet().iterator(); iterator.hasNext(); ) {
String key = (String) iterator.next();
Object obj = params.get(key);
if (obj == null) continue;
if (obj.getClass().isArray()) {
//数组的处理方式
Object[] objs = (Object[]) obj;
for (int i = 0; i < objs.length; i++) {
param.append(key).append("=").append(objs[i]);
param.append("&");
}
} else {
param.append(key).append("=").append(obj);
param.append("&");
}
}
out.print(param.toString());
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
//result += line;
result.append(line);
}
} catch (Exception e) {
System.err.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
return "{\"success\": false, \"message\": \"" + e.getMessage() + "\" }";
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result.toString();
}
}