Http四种请求方式:post ,get ,put,delete

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Http四种请求方式:post ,get ,put,delete相关的知识,希望对你有一定的参考价值。

  一.POST

package com.clw.drp.http;

import java.util.List;

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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HttpPostThread extends Thread {
  private static final String TAG="HttpPostThread";
  private Handler handle = null;
  String url = null;
  String token = null;
  String contentInfo = null;
  List<NameValuePair> paramList = null;

  // 构造函数
  public HttpPostThread(Handler hander) {
    handle = hander;
  }

  /**
   * 启动线程
   */
  public void doStart(String url, String token, String contentInfo, List<NameValuePair> paramList) {
    this.url = url;
    this.token = token;
    this.contentInfo = contentInfo;
    this.paramList = paramList;
    this.start(); 
  }

  /**
   * 线程运行
   */
  @Override
  public void run() {
    super.run();
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    String response = "";
    try {
      httpPost.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
      httpPost.setHeader("authorization", "Bearer " + this.token);
      if (null != contentInfo) {
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(contentInfo, HTTP.UTF_8));
      } else {
        httpPost.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8));
      }
      HttpResponse httpResponse = httpClient.execute(httpPost);
      Log.i(TAG, "调用POST请求------------------------------------");
      
      int statusCode = httpResponse.getStatusLine().getStatusCode();
      if (statusCode == HttpStatus.SC_OK) {
        response = EntityUtils.toString(httpResponse.getEntity());
      } else {
        response = "返回码:" + statusCode;
      }
    } catch (Exception e) {
      e.printStackTrace();
      response = "timeOut";
    }
    Bundle bundle = new Bundle();
    bundle.putString("data", response);
    Message message = handle.obtainMessage();
    message.setData(bundle);
    handle.sendMessage(message);
  }

}

 

二.GET

package com.clw.drp.http;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class HttpGetThread extends Thread {
    private Handler handle = null;
    String url = null;
    String token = null;

    // 构造函数
    public HttpGetThread(Handler hander) {
        handle = hander;
    }

    /**
     * 启动线程
     */
    public void doStart(String url, String token) {
        this.url = url;
        this.token = token;
        this.start();
    }

    /**
     * 线程运行
     */
    @Override
    public void run() {
        super.run();
        System.out.println("--------------------调用get请求");
        String response = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse;
        try {
            httpGet.setHeader("authorization", "Bearer " + this.token);
            httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                response = EntityUtils.toString(httpResponse.getEntity());
            } else {
                response = "返回码:" + statusCode;
            }
        
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            response = "timeOut";
        } catch (IOException e) {
            e.printStackTrace();
            response = "timeOut";
        }
        Bundle bundle = new Bundle();
        bundle.putString("data", response);
        Message message = handle.obtainMessage();
        message.setData(bundle);
        handle.sendMessage(message);
    }

}

 

三.PUT

package com.clw.drp.http;

import java.util.List;

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.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class HttpPutThread extends Thread {
  @SuppressWarnings("unused")
  private static final String TAG = "HttpPutThread";
  private Handler handler = null;
  String url = null;
  String token = null;
  String contentInfo = null;
  List<NameValuePair> paramList = null;

  public HttpPutThread(Handler handler) {
    this.handler = handler;
  }

  /**
   * 启动线程
   */
  public void doStart(String url, String token, String contentInfo, List<NameValuePair> paramList) {
    this.url = url;
    this.token = token;
    this.contentInfo = contentInfo;
    this.paramList = paramList;
    this.start();
  }

  @Override
  public void run() {
    super.run();
    String response = null;

    HttpClient httpClient = new DefaultHttpClient();
    HttpPut httpPut = new HttpPut(url);
    try {
      httpPut.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
      httpPut.setHeader("authorization", "Bearer " + this.token);
      if (null != contentInfo) {
        httpPut.setHeader("Content-Type", "application/json");
        httpPut.setEntity(new StringEntity(contentInfo, HTTP.UTF_8));
      } else {
        httpPut.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8));
      }
      HttpResponse httpResponse = httpClient.execute(httpPut);

      int statusCode = httpResponse.getStatusLine().getStatusCode();
      if (statusCode == HttpStatus.SC_OK) {
        response = EntityUtils.toString(httpResponse.getEntity());
      } else {
        response = "返回码:" + statusCode;
      }
    } catch (Exception e) {
      e.printStackTrace();
      response = "timeOut";
    }
    Bundle bundle = new Bundle();
    bundle.putString("data", response);
    Message message = handler.obtainMessage();
    message.setData(bundle);
    handler.sendMessage(message);
  }
}

 

四.DELETE

package com.clw.drp.http;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class HttpDeleteThread extends Thread {
  @SuppressWarnings("unused")
  private static final String TAG = "HttpDeleteThread";

  private Handler handle = null;
  String url = null;
  String token = null;

  // 构造函数
  public HttpDeleteThread(Handler hander) {
    handle = hander;
  }

  /**
   * 启动线程
   */
  public void doStart(String url, String token) {
    this.url = url;
    this.token = token;
    this.start();
  }

  @Override
  public void run() {
    super.run();
    String response = null;
    HttpClient client = new DefaultHttpClient();
    HttpDelete delete = new HttpDelete(url);
    HttpResponse httpResponse;
    try {
      delete.setHeader("authorization", "Bearer " + this.token);
      httpResponse = client.execute(delete);
      int statusCode = httpResponse.getStatusLine().getStatusCode();
      if (statusCode == HttpStatus.SC_OK) {
        response = EntityUtils.toString(httpResponse.getEntity());
      } else {
        response = "返回码:" + statusCode;
      }

    } catch (ClientProtocolException e) {
      e.printStackTrace();
      response = "timeOut";
    } catch (IOException e) {
      e.printStackTrace();
      response = "timeOut";
    }
    Bundle bundle = new Bundle();
    bundle.putString("data", response);
    Message message = handle.obtainMessage();
    message.setData(bundle);
    handle.sendMessage(message);
  }
}

 

以上是关于Http四种请求方式:post ,get ,put,delete的主要内容,如果未能解决你的问题,请参考以下文章

http请求中get和post请求的区别

POST提交数据的四种方式

四种常见的 POST 提交数据方式

四种常见的 POST 提交数据方式

get和post请求的区别

转四种常见的post请求