1.HttpURLConnection
java.net包中的HttpURLConnection
2. HttpGet,HttpPost
org.apache.http包中的HttpGet和HttpPost類(lèi)
HttpURLConnection
Get方法
public static void requestByGet() throws Exception {
String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建一個(gè)URL工具
URL url = new URL(path);
// 打開(kāi)一個(gè)HttpURLConnection連接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 設(shè)置超時(shí)時(shí)間
urlConn.setConnectTimeout(5 * 1000);
// 發(fā)起連接
urlConn.connect();
// 判斷請(qǐng)求狀態(tài) 200成功
if (urlConn.getResponseCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_GET, "Get方法要求樂(lè)成抢呆,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_GET, new String(data, "UTF-8"));
} else {
Log.i(TAG_GET, "Get方法要求掉敗");
}
// 關(guān)閉
urlConn.disconnect();
}
Post方法
public static void requestByPost() throws Throwable {
String path = "https://reg.163.com/logins.jsp";
// 要求的參數(shù)轉(zhuǎn)換為byte數(shù)組
String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")+ "&pwd=" + URLEncoder.encode("android", "UTF-8");
byte[] postData = params.getBytes();
// 新建一個(gè)URL工具
URL url = new URL(path);
// 打開(kāi)一個(gè)HttpURLConnection毗連
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 設(shè)置超時(shí)時(shí)間
urlConn.setConnectTimeout(5 * 1000);
// Post要求必需設(shè)置許可輸出
urlConn.setDoOutput(true);
// Post要求不克不及應(yīng)用緩存
urlConn.setUseCaches(false);
// 設(shè)置為Post要求
urlConn.setRequestMethod("POST");
urlConn.setInstanceFollowRedirects(true);
// 設(shè)置要求Content-Type
urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencode");
// 發(fā)起連接
urlConn.connect();
// 發(fā)送要求參數(shù)
DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
dos.write(postData);
dos.flush();
dos.close();
// 連接是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_POST, "Post要求方法樂(lè)成迄汛,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_POST, new String(data, "UTF-8"));
} else {
Log.i(TAG_POST, "Post方法要求掉敗");
}
}
HttpGet HttpPost
HttpGet方法
public static void requestByHttpGet() throws Exception {
String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建HttpGet工具
HttpGet httpGet = new HttpGet(path);
// 獲取HttpClient工具
HttpClient httpClient = new DefaultHttpClient();
// 獲取HttpResponse實(shí)例
HttpResponse httpResp = httpClient.execute(httpGet);
// 判斷連接是否成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpGet方法要求樂(lè)成胚委,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpGet方法要求掉敗");
}
}
HttpPost方法要求
public static void requestByHttpPost() throws Exception {
String path = "https://reg.163.com/logins.jsp";
// 新建HttpPost工具
HttpPost httpPost = new HttpPost(path);
// Post參數(shù)
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "helloworld"));
params.add(new BasicNameValuePair("pwd", "android"));
// 設(shè)置字符集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 設(shè)置參數(shù)實(shí)體
httpPost.setEntity(entity);
// 獲取HttpClient工具
HttpClient httpClient = new DefaultHttpClient();
// 獲取HttpResponse實(shí)例
HttpResponse httpResp = httpClient.execute(httpPost);
// 判斷請(qǐng)求是否成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpPost方法要求樂(lè)成士飒,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpPost方法要求掉敗");
}
}