一、HttpClinet方式
1、HTTP GET 示例:
- public class TestHttpGetMethod{
- public void get(){
- BufferedReader in = null;
- try{
- HttpClient client = new DefaultHttpClient();
- HttpGet request = new HttpGet();
- request.setURI("http://w26.javaeye.com");
- HttpResponse response = client.execute(request);
- in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while((line = in.readLine()) != null){
- sb.append(line + NL);
- }
- in.close();
- String page = sb.toString();
- Log.i(TAG, page);
- }catch(Exception e){
- Log.e(TAG,e.toString())
- }finally{
- if(in != null){
- try{
- in.close();
- }catch(IOException ioe){
- Log.e(TAG, ioe.toString());
- }
- }
- }
- }
- }
带参数的 HTTP GET:
- HttpGet request = new HttpGet("http://www.baidu.com/s?wd=amos_tl");
- client.execute(request);
2、HTTP POST 示例:
- public class TestHttpPostMethod{
- public void post(){
- BufferedReader in = null;
- try{
- HttpClient client = new DefaultHttpClient();
- HttpPost request = new HttpPost("http://localhost/upload.jsp");
- List<NameValuePair> postParams = new ArrayList<NameValuePair>();
- postParams.add(new BasicNameValuePair("filename", "sex.mov"));
- UrlEncodeFormEntity formEntity = new UrlEncodeFormEntity(postParams);
- request.setEntity(formEntity);
- HttpResponse response = client.execute(request);
- in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while((line = in.readLine()) != null){
- sb.append(line + NL);
- }
- in.close();
- String result = sb.toString();
- Log.i(TAG, result );
- }catch(Exception e){
- Log.e(TAG,e.toString())
- }finally{
- if(in != null){
- try{
- in.close();
- }catch(IOException ioe){
- Log.e(TAG, ioe.toString());
- }
- }
- }
- }
- }
二、HttpURLConnection 方式
- URL url = null;
- HttpURLConnection conn = null;
- InputStream in = null;
- OutputStream out = null;
- byte[] data ="测试字符串".getBytes();
- try{
- url =new URL("www.xxx.com/servlet");
- conn = (HttpURLConnection) url.openConnection();
- //设置连接属性
- conn.setDoOutput(true);// 使用 URL 连接进行输出
- conn.setDoInput(true);// 使用 URL 连接进行输入
- conn.setUseCaches(false);// 忽略缓存
- conn.setConnectTimeout(30000);//设置连接超时时长,单位毫秒
- conn.setRequestMethod("POST");//设置请求方式,POST or GET,注意:如果请求地址为一个servlet地址的话必须设置成POST方式
- //设置请求头
- conn.setRequestProperty("Accept", "*/*");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.setRequestProperty("Accept-Charset", "utf-8");
- if (data != null) {
- out = conn.getOutputStream();
- out.write(data);
- }
- int code = conn.getResponseCode();
- if(code ==200){
- in = conn.getInputStream();// 可能造成阻塞
- long len = conn.getContentLength();
- byte[] bs = new byte[(int) len];//返回结果字节数组
- int all = 0;
- int dn = 0;
- while ((dn = in.read(bs, all, 1)) > 0) {
- all += dn;
- if (all == len) {
- break;
- }
- }
- }
- }