package com.sdutacm.sportacm;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
/**
* Created by bummer on 2017/8/1.
*/
public class HttpUtil {
static HttpURLConnection conn = null;
static InputStreamReader input = null;
static BufferedReader buffer = null;
static PrintWriter print = null;
public static void httpconnect(String url){
try {
URL httpUrl = new URL(url);
conn = (HttpURLConnection) httpUrl.openConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String Get(){
String inputLine = null;
String result = "";
try {
input = new InputStreamReader(conn.getInputStream());
buffer = new BufferedReader(input);
while ((inputLine = buffer.readLine())!=null){
result +=inputLine+"\n";
}
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static String post(String params){
String result = "";
try {
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
OutputStream out = conn.getOutputStream();
print = new PrintWriter(out);
print.print(params);
print.flush();
InputStreamReader input = new InputStreamReader(conn.getInputStream());
BufferedReader reader = new BufferedReader(input);
String inputLine = null;
while ((inputLine = reader.readLine())!=null){
result += inputLine +"\n";
}
print.close();
conn.connect();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void close(){
if(conn != null){
conn.disconnect();
}
}
public static String clientpost(String url, List<NameValuePair> params){
//发送POST请求,创建HttpPost对象
HttpPost httpRequest = new HttpPost(url);
HttpEntity httpEntity;
String result = "";
try {
httpEntity = new UrlEncodedFormEntity( params,"utf-8");
httpRequest.setEntity(httpEntity);
/*HttpClient 是Http的一个借口,因此,HttpClient对象需要通过DefaultHttpClient来进行实例化
* */
HttpClient httpClient = new DefaultHttpClient();
//发送请求,获取响应 该方法将获得Http的响应
HttpResponse httpResponse = httpClient.execute(httpRequest);
//判断请求和相应是否成功
if(httpResponse.getStatusLine().getStatusCode() ==
HttpStatus.SC_OK){
//调用EntityUtils.toString()方法可获取封装在HttpEntity对象中的服务器的响应内容
result = EntityUtils.toString(httpResponse.getEntity());
}else {
result = "请求错误";
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
HttpUtils.java
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
HttpUtils——助力高效网络通信
【代码】HttpUtils——助力高效网络通信。
知识图谱 人工智能 java 开发语言 服务器 -
HttpUtils工具类
"HttpUtils工具类"
json apache java .net 工具类