1. @Test
  2. public void doPost() throws Exception {
  3.  
  4. CloseableHttpClient httpClient = HttpClients.createDefault();
  5. // HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");
  6. // 注意:如果请求的url后缀是 .html则浏览器不能返回正确的json数据,会返回406错误,所以需要修改请求url的后缀为其他
  7. HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
  8. CloseableHttpResponse response = httpClient.execute(post);
  9. HttpEntity entity = response.getEntity();
  10. String string = EntityUtils.toString(entity, "utf-8");
  11. System.out.println(string);
  12. response.close();
  13. httpClient.close();
  14. }

项目的web.xml中最后加入

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3. <display-name>taotao-portal</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.html</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12.  
  13. <!-- 加载spring容器 -->
  14. <context-param>
  15. <param-name>contextConfigLocation</param-name>
  16. <param-value>classpath:spring/applicationContext-*.xml</param-value>
  17. </context-param>
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. <!-- 解决post乱码 -->
  22. <filter>
  23. <filter-name>CharacterEncodingFilter</filter-name>
  24. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  25. <init-param>
  26. <param-name>encoding</param-name>
  27. <param-value>utf-8</param-value>
  28. </init-param>
  29. </filter>
  30. <filter-mapping>
  31. <filter-name>CharacterEncodingFilter</filter-name>
  32. <url-pattern>/*</url-pattern>
  33. </filter-mapping>
  34. <!-- springmvc的前端控制器 -->
  35. <servlet>
  36. <servlet-name>taotao-portal</servlet-name>
  37. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  38. <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
  39. <init-param>
  40. <param-name>contextConfigLocation</param-name>
  41. <param-value>classpath:spring/springmvc.xml</param-value>
  42. </init-param>
  43. <load-on-startup>1</load-on-startup>
  44. </servlet>
  45. <!-- 伪静态化 -->
  46. <servlet-mapping>
  47. <servlet-name>taotao-portal</servlet-name>
  48. <url-pattern>*.html</url-pattern>
  49. </servlet-mapping>
  50.  
  51. <!-- 配置需要返回json类型请求的拦截 -->
  52. <servlet-mapping>
  53. <servlet-name>taotao-portal</servlet-name>
  54. <url-pattern>*.action</url-pattern>
  55. </servlet-mapping>
  56. </web-app>

taotao服务测试http请求需要返回json时出现406错误处理的更多相关文章

  1. Query通过Ajax向PHP服务端发送请求并返回JSON数据

    Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...

  2. jQuery通过Ajax向PHP服务端发送请求并返回JSON数据

    SON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.JSON在前后台交互的过程中发挥着相当出色的作用.请接着往下看教 ...

  3. Spring MVC 返回json数据 报406错误 问题解决方案

    将jackson jar包改为jackson-databind-2.5.0.jar  jackson-core-2.5.0.jar  jackson-annotations-2.5.0.jar(这个版 ...

  4. Spring Security 前后端分离登录,非法请求直接返回 JSON

    hello 各位小伙伴,国庆节终于过完啦,松哥也回来啦,今天开始咱们继续发干货! 关于 Spring Security,松哥之前发过多篇文章和大家聊聊这个安全框架的使用: 手把手带你入门 Spring ...

  5. 七:Spring Security 前后端分离登录,非法请求直接返回 JSON

    Spring Security 前后端分离登录,非法请求直接返回 JSON 解决方案 在 Spring Security 中未获认证的请求默认会重定向到登录页,但是在前后端分离的登录中,这个默认行为则 ...

  6. jquery的ajax异步请求接收返回json数据

    http://www.jb51.net/article/51122.htm jquery的ajax异步请求接收返回json数据方法设置简单,一个是服务器处理程序是返回json数据,另一种就是ajax发 ...

  7. 根据传入url请求,返回json字符串

    /** * 根据传入url请求,返回json字符串 * @param url * @return * @throws UnsupportedEncodingException */ public st ...

  8. ajax返回json时,js获取类型,是字符串类型

    ajax向php服务端请求,并返回json串给前端. js发现得到的返回值的类型是字符串,不能直接取json对象属性,需要JSON.parse(). 怎么解决呢? 这需要在php返回json时,加上一 ...

  9. Spring WebMVC 4.1返回json时 406(Not Acceptable)

    1.问题现象Tomcat7+Spring4.1.4,返回json字符串时发生406错误 The resource identified by this request is only capable ...

随机推荐

  1. [JSON].set(keyPath, value)

    语法:[JSON].set( keyPath, value ) 返回:[True | False] 说明:设置键值 参数: keyPath    [keyPath 必需] 键名路径字符串 value ...

  2. 245. Subtree【LintCode java】

    Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  3. 【转】从零开始学习Skynet_examples研究

    转自 http://blog.csdn.net/mr_virus/article/details/52330193 一.编译Skynet: 1.用ubuntu15.10直接 make linux 编译 ...

  4. 数据库Mysql的学习(三)-各种约束

    删除数据库表 drop table [if exists] 表一,表二.....; 表分区:比如图书信息表有1000万个图书信息,如何优化他,其中一种方式就是表分区.就是把一张表的数据分成多个区块,这 ...

  5. 第一个线性回归程序(基于Jupyter)

    import pandas as pdimport seaborn as snssns.set(context="notebook", style="whitegrid& ...

  6. OpenPAI大规模人工智能平台安装部署文档

    环境要求: 如果需要图形界面,需要在Ubuntu系统安装,否则centos系统安装时是没有问题的(web端和命令行进行任务提交) 安装过程需要有另外一台控制端机器(注意:区别于集群所在的任何一台服务器 ...

  7. Training Models

    In this page, I am going to talk about the 'hello world' model that is linear regression and train i ...

  8. leetcode个人题解——#19 Remove Nth Node From End of List

    思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作. 当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分 ...

  9. Mininet实验 MAC地址学习分析

    拓扑图 学习过程分析 首先交换机A和交换机B一开始的MAC地址表都是空的. 此时主机11向主机33发送一个数据帧. 数据帧会先到达交换机A,交换机A会获得主机11的MAC地址和端口号.(此时交换机A的 ...

  10. eg_2

    2. 编写一个程序,输出在一个字符串中,指定的字符串出现的次数 第一种方法: public class Test { public static void main(String[] args) { ...