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+"
";
}
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 +"
";
}
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){
HttpPost httpRequest = new HttpPost(url);
HttpEntity httpEntity;
String result = "";
try {
httpEntity = new UrlEncodedFormEntity( params,"utf-8");
httpRequest.setEntity(httpEntity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode() ==
HttpStatus.SC_OK){
result = EntityUtils.toString(httpResponse.getEntity());
}else {
result = "请求错误";
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}