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);
  }
}
时间: 2024-10-09 20:38:23

Http四种请求方式:post ,get ,put,delete的相关文章

同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式

1. 概念理解        在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:   同步/异步主要针对C端: 同步:      所谓同步,就是在c端发出一个功能调用时,在没有得到结果之前,该调用就不返回.也就是必须一件一件事做,等前一件做完了才能做下一件事.   例如普通B/S模式(同步):提交请求->等待服务器处理->处理完毕返回 这个期间客户端浏览器不能干任何事 异步:      异步的概念和同步相对.当c端一个

msyql四种启动方式

1 mysql默认启动配置文件my.cnf顺序 第一步:/etc/my.cnf 第二步:/etc/mysql/my.cnf 第三步:/usr/local/mysql/etc/my.cnf 第四步:~/.my.cnf 可以通过命令查看加载顺序: [[email protected] ~]# which mysqld /usr/local/mysql/bin/mysqld [[email protected] ~]# /usr/local/mysql/bin/mysqld --verbose --h

python写http post请求的四种请求体

Web自动化测试(25) HTTP 协议规定 POST 提交的数据必须放在消息主体(entity-body)中,但协议并没有规定数据必须使用什么编码方式.常见的四种编码方式如下: 1.application/x-www-form-urlencoded 这应该是最常见的 POST 提交数据的方式了.浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据.请求类似于下面这样(无关的请求头在本文中

Nginx 四种分配方式——session处理

最近迷上了Nginx,真实麻雀虽小,五脏俱全..功能实在强大.. nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态.静态页面的分离,可以按照轮询.ip哈希.URL哈希.权重等多种方式对后端服务器做负载均衡,同时还支持后端服务器的健康检查. 如果只有一台服务器时,这个服务器挂了,那么对于网站来说是个灾难.因此,这时候的负载均衡就会大显身手了,它会自动剔除挂掉的服务器. 下面简单的介绍下我使用Nginx做负载的体会 下载---安装Ngi

spring security四种实现方式

spring security四种实现方式 标签: spring security spring spring(20) > 目录(?)[+] 最简单配置spring-securityxml实现1 实现UserDetailsService 实现动态过滤用户权限 实现AuthenticationProvider自定义参数验证 spring security实现方式大致可以分为这几种: 1.配置文件实现,只需要在配置文件中指定拦截的url所需要权限.配置userDetailsService指定用户名.

HTTP 8种请求方式介绍

HTTP是超文本传输协议,其定义了客户端与服务器端之间文本传输的规范.HTTP默认使用80端口,这个端口指的是服务端的端口,而客户端使用的端口是动态分配的.当我们没有指定端口访问时,浏览器会默认帮我们添加80端口.我们也可以自己指定访问端口如:http://www.ip138.com:80. 需要注意的是,现在大多数访问都使用了HTTPS协议,而HTTPS的默认端口为443,如果使用80端口访问HTTPS协议的服务器可能会被拒绝. HTTP请求的方法: HTTP/1.1协议中共定义了八种方法(有

同步、异步、阻塞、非阻塞四种调用方式

在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 同步/异步主要针对C端:  同步(Sync) 所谓同步,就是发出一个功能调用时,在没有得到结果之前,该调用就不返回或继续执行后续操作. 根据这个定义,Java中所有方法都是同步调用,应为必须要等到结果后才会继续执行.我们在说同步.异步的时候,一般而言是特指那些需要其他端协作或者需要一定时间完成的任务. 简单来说,同步就是必须一件一件事做,等前一件做完了才能做下一件事.

利用图形窗口分割法将极坐标方程:r=cos(θ/3)+1/9用四种绘图方式画在不同的窗口中

利用图形窗口分割法将极坐标方程:r=cos(θ/3)+1/9用四种绘图方式画在不同的窗口中. 解:MATLAB指令: theta=0:0.1:6*pi;rho=cos(theta/3)+1/9; >> polar(theta,rho) >> >> plot(theta,rho) >> semilogx(theta,rho) >> grid >> hist(rho,15) 结果分别如下图: 图1 图2 图3 图4

mysql的四种启动方式

mysql的四种启动方式: 1.mysqld 启动mysql服务器:./mysqld --defaults-file=/etc/my.cnf --user=root 客户端连接: mysql --defaults-file=/etc/my.cnf or mysql -S /tmp/mysql.sock 2.mysqld_safe 启动mysql服务器:./mysqld_safe --defaults-file=/etc/my.cnf --user=root & 客户端连接: mysql --de