47 lines
1.2 KiB
Plaintext
47 lines
1.2 KiB
Plaintext
package com.jysoft.weChat.util;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
|
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
/*import com.alibaba.fastjson.JSONObject;*/
|
|
|
|
public class HttpUtilComputer {
|
|
|
|
public static JSONObject getResult(String requestUrl) throws Exception {
|
|
String encoding = "UTF-8";
|
|
URL url = new URL(requestUrl);
|
|
// 打开和URL之间的连接
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
// 设置通用的请求属性
|
|
connection.setRequestProperty("Connection", "Keep-Alive");
|
|
// 不使用缓冲
|
|
connection.setUseCaches(false);
|
|
// 允许输出流,即允许上传
|
|
connection.setDoOutput(true);
|
|
// 允许下载
|
|
connection.setDoInput(true);
|
|
|
|
// 建立实际的连接
|
|
connection.connect();
|
|
// 定义 BufferedReader输入流来读取URL的响应
|
|
BufferedReader in = null;
|
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));
|
|
String result = "";
|
|
String getLine;
|
|
while ((getLine = in.readLine()) != null) {
|
|
result += getLine;
|
|
}
|
|
in.close();
|
|
JSONObject jsonobject = JSONObject.fromObject(result);
|
|
|
|
return jsonobject;
|
|
}
|
|
|
|
}
|