Http 四種請求訪問程式碼 HttpGet HttpPost HttpPut HttpDelete
邏輯:
String url = "http://www.baidu.com";
//將要訪問的url字串放入HttpPost中
HttpPost httpPost = new HttpPost(url);
//請求頭 放置一些修改http請求頭和cookie
httpPost.setHeader("Accept", "application/json");
......
//如果是HttpPost或者HttpPut請求需要在請求里加引數,而HttpGet或者HttpDelete請求則可以直接拼接到url字串後面
//向HttpPost中加入引數
List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add(new NameValuePair("id", "1"));
values.add(new NameValuePair("name", "xiaohong"));
httpPost.setEntity(new UrlEncodeFormEntity(values, HTTP.UTF_8)); //進行轉碼
//例項HttpClient 並執行帶有HttpPost的方法,返回HttpResponse 響應,再進行操作
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode(); //返回狀態碼 ,用來進行識別或者判斷訪問結果
if(statusCode == 200){
Instream in = httpResponse.getEntity().getContent(); //要處理該資料流是否為GZIP流
}
示例程式碼如下:
package cn.dratek.haoyingsheng.manager.client;
import cn.dratek.haoyingsheng.manager.util.ResourceUtil;
import net.dratek.browser.http.Cookie;
import net.dratek.browser.http.CookieManager;
import net.dratek.browser.http.URL;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class HttpNetClient {
/**
* 所有get 請求底層呼叫方法
*
* @param url 請求url
* @return byte[] response data
*/
public static byte[] doGet(String url) {
InputStream in;
byte[] bre = null;
HttpResponse response;
CookieManager manager = CookieManager.getInstance();
if (url != null && url.length() != 0) {
URL myURL = URL.parseString(url);
Cookie[] cookies = manager.getCookies(myURL);
HttpGet httpGet = new HttpGet(url);
if (cookies != null && cookies.length > 0) {
StringBuilder sb = new StringBuilder();
for (Cookie ck : cookies) {
sb.append(ck.name).append('=').append(ck.value).append(";");
}
String sck = sb.toString();
if (sck.length() > 0) {
httpGet.setHeader("Cookie", sck);
}
}
httpGet.setHeader("Accept-Encoding", "gzip, deflate");
httpGet.setHeader("Accept-Language", "zh-CN");
httpGet.setHeader("Accept", "application/json, application/xml, text/html, text/*, image/*, */*");
try {
response = new DefaultHttpClient().execute(httpGet);
if (response != null) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (Header header : headers) {
manager.setCookie(myURL, header.getValue());
}
}
in = response.getEntity().getContent();
if (in != null) {
bre = ResourceUtil.readStream(in);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bre;
}
/**
* 所有Post 請求底層呼叫方法
*
* @param url 請求url
* @param values 傳遞的引數
* @return byte[] 返回資料 or null
*/
public static byte[] doPost(String url, List<NameValuePair> values) {
System.out.println("url = " + url);
byte[] bytes = null;
HttpResponse response;
InputStream inputStream = null;
CookieManager manager = CookieManager.getInstance();
if (url != null && url.length() != 0) {
URL myurl = URL.parseString(url);
Cookie[] cookies = manager.getCookies(myurl);
HttpPost post = new HttpPost(url);
if (cookies != null && cookies.length > 0) {
StringBuilder sb = new StringBuilder();
for (Cookie ck : cookies) {
sb.append(ck.name).append('=').append(ck.value).append(";");
}
String sck = sb.toString();
if (sck.length() > 0) {
post.setHeader("Cookie", sck);
}
}
post.setHeader("Accept-Encoding", "gzip, deflate");
post.setHeader("Accept-Language", "zh-CN");
post.setHeader("Accept", "application/json, application/xml, text/html, text/*, image/*, */*");
DefaultHttpClient client = new DefaultHttpClient();
try {
if (values != null && values.size() > 0) {
post.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8));
}
response = client.execute(post);
if (response != null) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (Header header : headers) {
manager.setCookie(myurl, header.getValue());
}
}
inputStream = response.getEntity().getContent();
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (inputStream != null) {
bytes = ResourceUtil.readStream(inputStream);
}
}
return bytes;
}
/**
* PUT基礎請求
*
* @param url 請求地址
* @param values 提交引數
* @return byte[] 請求成功後的結果
*/
public static byte[] doPut(String url, List<NameValuePair> values) {
byte[] ret = null;
CookieManager manager = CookieManager.getInstance();
if (url != null && url.length() > 0) {
URL myUrl = URL.parseString(url);
StringBuilder sb = new StringBuilder();
Cookie[] cookies = manager.getCookies(myUrl);
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
sb.append(cookie.name).append("=").append(cookie.value).append(";");
}
}
HttpPut request = new HttpPut(url);
String sck = sb.toString();
if (sck.length() > 0) {
request.setHeader("Cookie", sck);
}
request.setHeader("Accept-Encoding", "gzip, deflate");
request.setHeader("Accept-Language", "zh-CN");
request.setHeader("Accept", "application/json, application/xml, text/html, text/*, image/*, */*");
DefaultHttpClient client = new DefaultHttpClient();
if (values != null && values.size() > 0) {
try {
UrlEncodedFormEntity entity;
entity = new UrlEncodedFormEntity(values);
request.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
try {
HttpResponse response = client.execute(request);
if (response != null) {
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200 || statusCode == 403) {
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (Header header : headers) {
manager.setCookie(myUrl, header.getValue());
}
}
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
if (inputStream != null) {
ret = ResourceUtil.readStream(inputStream);
inputStream.close();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
}
/**
* Delete基礎請求
*
* @param url 請求地址
* @return 請求成功後的結果
*/
public static byte[] doDelete(String url) {
InputStream in;
byte[] bre = null;
HttpResponse response;
CookieManager manager = CookieManager.getInstance();
if (url != null && url.length() != 0) {
URL myurl = URL.parseString(url);
Cookie[] cookies = manager.getCookies(myurl);
HttpDelete delete = new HttpDelete(url);
if (cookies != null && cookies.length > 0) {
StringBuilder sb = new StringBuilder();
for (Cookie ck : cookies) {
sb.append(ck.name).append('=').append(ck.value).append(";");
}
String sck = sb.toString();
if (sck.length() > 0) {
delete.setHeader("Cookie", sck);
}
}
delete.setHeader("Accept-Encoding", "gzip, deflate");
delete.setHeader("Accept-Language", "zh-CN");
delete.setHeader("Accept", "application/json, application/xml, text/html, text/*, image/*, */*");
try {
response = new DefaultHttpClient().execute(delete);
if (response != null) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (Header header : headers) {
manager.setCookie(myurl, header.getValue());
}
}
in = response.getEntity().getContent();
if (in != null) {
bre = ResourceUtil.readStream(in);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bre;
}
}