public class HttpUtil {
public static String sendDataByHttpClientGet(String path,String name,String pass){
String result = "";
//1.獲取到一個瀏覽器
HttpClient client = new DefaultHttpClient();
//2.準備請求的地址
try {
String arg1 = URLEncoder.encode(name, "utf-8");
String arg2 = URLEncoder.encode(pass, "utf-8");
HttpGet httpGet = new HttpGet(path+"?name="+arg1+"&pass="+arg2);
//3.敲回車發請求
HttpResponse resp = client.execute(httpGet);
//狀態碼
int code = resp.getStatusLine().getStatusCode();
if(code==200){
//resp.getEntity().getContent();
result = EntityUtils.toString(resp.getEntity(),"utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static String sendDataByHttpClientPost(String path,String name,String pass){
String result = "";
//1獲取到一個瀏覽器
HttpClient client = new DefaultHttpClient();
//2.準備要請求的數據類型
HttpPost httpPost = new HttpPost(path);
try {
//鍵值對 NameValuePair
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name",name));
params.add(new BasicNameValuePair("pass", pass));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");
//3.設置POST請求數據實體
httpPost.setEntity(entity);
//4.發送數據給服務器
HttpResponse resp = client.execute(httpPost);
int code = resp.getStatusLine().getStatusCode();
if(code==200){
result = EntityUtils.toString(resp.getEntity(),"utf-8");
}
} catch (Exception e) {
}
return result;
}
}