HttpClient的支持在HTTP/1.1规范中定义的所有的HTTP方法:GET, HEAD, POST, PUT, DELETE, TRACE 和 OPTIONS。每有一个方法都有一个对应的类:HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace和HttpOptions。所有的这些类均实现了HttpUriRequest接口,故可以作为execute的执行参数使用。请求URI是能够应用请求的统一资源标识符。 HTTP请求的URI包含一个协议计划protocol scheme,主机名host name,,可选的端口optional port,资源的路径resource path,可选的查询optional query和可选的片段optional fragment。

head,put,delete,trace HttpClient支持这些方法,
大多数浏览器不支持这些方法,原因是Html 4中对 FORM 的method方法只支持两个get和post,很多浏览器还都依然是基于html4的。

通常会在JAVA中通过代码调用URL进行远端方法调用,这些方法有的是Get请求方式的,有的是POST请求方式的,为此,总结一例,贴出以便查阅。

依赖JAR包如下图:

示例代码:

  1. package com.wujintao.httpclient;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
  5. import org.apache.commons.httpclient.HttpClient;
  6. import org.apache.commons.httpclient.HttpException;
  7. import org.apache.commons.httpclient.HttpStatus;
  8. import org.apache.commons.httpclient.NameValuePair;
  9. import org.apache.commons.httpclient.methods.GetMethod;
  10. import org.apache.commons.httpclient.methods.PostMethod;
  11. import org.apache.commons.httpclient.params.HttpMethodParams;
  12. import org.junit.Test;
  13. public class TestCase {
  14. @Test
  15. public void testGetRequest() throws IllegalStateException, IOException {
  16. HttpClient client = new HttpClient();
  17. StringBuilder sb = new StringBuilder();
  18. InputStream ins = null;
  19. // Create a method instance.
  20. GetMethod method = new GetMethod("http://www.baidu.com");
  21. // Provide custom retry handler is necessary
  22. method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  23. new DefaultHttpMethodRetryHandler(3, false));
  24. try {
  25. // Execute the method.
  26. int statusCode = client.executeMethod(method);
  27. System.out.println(statusCode);
  28. if (statusCode == HttpStatus.SC_OK) {
  29. ins = method.getResponseBodyAsStream();
  30. byte[] b = new byte[1024];
  31. int r_len = 0;
  32. while ((r_len = ins.read(b)) > 0) {
  33. sb.append(new String(b, 0, r_len, method
  34. .getResponseCharSet()));
  35. }
  36. } else {
  37. System.err.println("Response Code: " + statusCode);
  38. }
  39. } catch (HttpException e) {
  40. System.err.println("Fatal protocol violation: " + e.getMessage());
  41. } catch (IOException e) {
  42. System.err.println("Fatal transport error: " + e.getMessage());
  43. } finally {
  44. method.releaseConnection();
  45. if (ins != null) {
  46. ins.close();
  47. }
  48. }
  49. System.out.println(sb.toString());
  50. }
  51. @Test
  52. public void testPostRequest() throws HttpException, IOException {
  53. HttpClient client = new HttpClient();
  54. PostMethod method = new PostMethod("http://www.baidu.com/getValue");
  55. method.setRequestHeader("Content-Type",
  56. "application/x-www-form-urlencoded;charset=gb2312");
  57. NameValuePair[] param = { new NameValuePair("age", "11"),
  58. new NameValuePair("name", "jay"), };
  59. method.setRequestBody(param);
  60. int statusCode = client.executeMethod(method);
  61. System.out.println(statusCode);
  62. method.releaseConnection();
  63. }
  64. }

HttpClient之Get请求和Post请求示例的更多相关文章

  1. httpclient就是个能发送http连接的工具包,包括能发送post请求和get请求

    1.httpclient就是个能发送http连接的工具包,包括能发送post请求和get请求. http 连接一次就有返回流.http是个双向的嘛.只有连接了,就会有输出返回流. 所以在执行http连 ...

  2. 03_Django-GET请求和POST请求-设计模式及模板层

    03_Django-GET请求和POST请求-设计模式及模板层 视频:https://www.bilibili.com/video/BV1vK4y1o7jH 博客:https://blog.csdn. ...

  3. Ajax中get请求和post请求

    我们在使用Ajax向服务器发送数据时,可以采用Get方式请求服务器,也可以使用Post方式请求服务器,那么什么时候该采用Get方式,什么时候该采用Post方式呢? Get请求和Post请求的区别: 1 ...

  4. slave IO流程之二:注册slave请求和dump请求

    slave IO流程已经在http://www.cnblogs.com/onlyac/p/5815566.html中有介绍 这次我们要探索注册slave请求和dump请求的报文格式和主要流程. 一.注 ...

  5. loadrunner录制脚本如何选择使用get请求和post请求的方式

    在loadrunner工具里录制脚本时常常会用到get请求和post请求,有关loadrunner常用的这两类的请求主要有: get请求: web_url 和 web_link post请求: web ...

  6. iOS开发网络篇—GET请求和POST请求

    iOS开发网络篇—GET请求和POST请求 一.GET请求和POST请求简单说明 创建GET请求 // 1.设置请求路径 NSString *urlStr=[NSString stringWithFo ...

  7. 普通请求和ajax请求的区别

    普通请求和ajax请求的区别? 下面的action返回一个json文件,文件内容为sts.*,data1

  8. iOS开发网络篇—GET请求和POST请求(转)

    一.GET请求和POST请求简单说明 创建GET请求 1 // 1.设置请求路径 2 NSString *urlStr=[NSString stringWithFormat:@"http:/ ...

  9. GET请求和POST请求的区别

    request获取请求参数 最为常见的客户端传递参数方式有两种: 浏览器地址栏直接输入:一定是GET请求: 超链接:一定是GET请求: 表单:可以是GET,也可以是POST,这取决与<form& ...

随机推荐

  1. Fiddler2 下断点修改HTTP报文

    一 Fiddler中设置断点修改HTTP请求 方法1:全局断点.Rules-->Automatic BreakPoint-->Before Requests(或快捷键F11),这种方法会拦 ...

  2. dfs 例题皇后问题

    题目描述 一个如下的 6 \times 66×6 的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列  ...

  3. 洛谷 P2568 GCD 题解

    原题链接 庆祝一下:数论紫题达成成就! 第一道数论紫题.写个题解庆祝一下吧. 简要题意:求 \[\sum_{i=1}^n \sum_{j=1}^n [gcd(i,j)==p] \] 其中 \(p\) ...

  4. Java 入门学习知识点整理

    [JAVA一个文件写多个类 ( 同级类 ) 规则和注意点] 在一个.java文件中可以有多个同级类,  其修饰符只可以public/abstract/final/和无修饰符 public修饰的只能有一 ...

  5. angular中$q用法, $q多个promise串行/同步/等待), $q.all用法,使用

    $q的基本用法 function fn() { var defer = $q.defer(); setTimeout(function () { console.log(1); defer.resol ...

  6. command > /dev/null command > /dev/null 2>&1nohup command &> /dev/null的区别

    1.对以下命令进行依次区分 command 执行一条普通的命令 command > /dev/null   '>'表示将标准输出重定向 '>>'表示追加,/dev/null是一 ...

  7. spring-cloud-gateway过滤器实践

    概述 这里是 SpringCloud Gateway 实践的第一篇,主要讲过滤器的相关实现.Spring-Cloud-Gateway 是以 WebFlux 为基础的响应式架构设计, 是异步非阻塞式的, ...

  8. 走近源码:Redis如何清除过期key

    "叮--",美好的周六就这么被一阵钉钉消息吵醒了. 业务组的同学告诉我说很多用户的帐号今天被强制下线.我们的帐号系统正常的逻辑是用户登录一次后,token的有效期可以维持一天的时间 ...

  9. 理解BERT:一个突破性NLP框架的综合指南

    概述 Google的BERT改变了自然语言处理(NLP)的格局 了解BERT是什么,它如何工作以及产生的影响等 我们还将在Python中实现BERT,为你提供动手学习的经验 BERT简介 想象一下-- ...

  10. Arcgis License的安装及破解

    1.双击LicenseManager安装目录下的Setup.exe. 2.点击“Next”. 3.选择“I accept the license agreement”,点击“Next”. 4.点击“C ...