码迷,mamicode.com
首页 > Web开发 > 详细

httpCilent代码

时间:2020-04-02 17:46:59      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:使用   print   throw   int   header   响应头   exec   request   lis   

httpUtil:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class HttpClient {

public String post(String requestUrl, String requestPram,String accessToken){
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
StringBuffer responseResult = new StringBuffer();
HttpURLConnection httpURLConnection = null;
try {
URL realUrl = new URL(requestUrl);
httpURLConnection = (HttpURLConnection) realUrl.openConnection();
httpURLConnection.setRequestProperty("accept", "*/*");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("connection", "Keep-Alive");
if(!accessToken.equals("")||accessToken!=null){
httpURLConnection.setRequestProperty("access-token", accessToken);
}
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length()));
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8");
outputStreamWriter.write(requestPram);
outputStreamWriter.flush();
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpURLConnection.disconnect();
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}

}
return responseResult.toString();
}

public String postJson(String requestUrl, String requestPram,String accessToken){
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
StringBuffer responseResult = new StringBuffer();
HttpURLConnection httpURLConnection = null;
try {
URL realUrl = new URL(requestUrl);
httpURLConnection = (HttpURLConnection) realUrl.openConnection();
httpURLConnection.setRequestProperty("accept", "*/*");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("connection", "Keep-Alive");
if(!accessToken.equals("")||accessToken!=null){
httpURLConnection.setRequestProperty("access-token", accessToken);
}
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length()));
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8");
outputStreamWriter.write(requestPram);
outputStreamWriter.flush();
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpURLConnection.disconnect();
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}

}
return responseResult.toString();
}

public String get(String url, String param) throws Exception {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
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.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
//System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
使用:
postJson:
HttpClient httpClient = new HttpClient();
String param = "{\"appKey\":\""+appKey+"\"," +
"\"signature\":\""+signature+"\"," +
"\"time\":\""+time+"\"}";
String resJson =httpClient.postJson(url+"/api/token",param,"");
JSONObject bodyJson = (JSONObject) JSONObject.parse(resJson);
get:
String cityPram = "appKey=yongjia_pro&token="+XinLingTask.getToken();
HttpClient httpTest = new HttpClient();
String cityRes = httpTest.get(XinLingTask.url+"/addition/577/5775/api/dailyCity",cityPram);


httpCilent代码

标签:使用   print   throw   int   header   响应头   exec   request   lis   

原文地址:https://www.cnblogs.com/jaycee/p/12621139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!