1. package com.cn.eport.util.common;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5.  
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.HttpStatus;
  9. import org.apache.http.StatusLine;
  10. import org.apache.http.client.ClientProtocolException;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.methods.CloseableHttpResponse;
  13. import org.apache.http.client.methods.HttpDelete;
  14. import org.apache.http.client.methods.HttpGet;
  15. import org.apache.http.client.methods.HttpPost;
  16. import org.apache.http.entity.StringEntity;
  17. import org.apache.http.impl.client.CloseableHttpClient;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.apache.http.impl.client.HttpClients;
  20. import org.apache.http.util.EntityUtils;
  21. import org.apache.log4j.Logger;
  22. import org.apache.poi.ss.formula.functions.T;
  23. import org.springframework.web.client.RestTemplate;
  24. import com.cn.eport.service.Web;
  25. public class webServiceUtil {
  26. static Logger logger = Logger.getLogger(Web.class);
  27.  
  28. /**
  29. * get请求
  30. *
  31. * @return
  32. */
  33. public static String doGet(String url) {
  34. try {
  35. HttpClient client = new DefaultHttpClient();
  36. // 发送get请求
  37. HttpGet request = new HttpGet(url);
  38. HttpResponse response = client.execute(request);
  39.  
  40. /** 请求发送成功,并得到响应 **/
  41. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  42. /** 读取服务器返回过来的json字符串数据 **/
  43. String strResult = EntityUtils.toString(response.getEntity());
  44.  
  45. return strResult;
  46. }
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50.  
  51. return null;
  52. }
  53. /**
  54. * 相当get请求
  55. *
  56. * @return
  57. */
  58. public List<T> getPortList() {
  59. // TODO Auto-generated method stub
  60. RestTemplate client = new RestTemplate();
  61. String url = "http://12.123.12.123:8080/abc/abcd";
  62. String portString = client.getForObject(url, String.class);
  63. JsonTools jsonTools = new JsonTools();
  64. String className = "com.cn.eport.pojo.Port";
  65. Class clazz = null;
  66. try {
  67. clazz = Class.forName(className);
  68. } catch (ClassNotFoundException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. }
  72. List<T> portList = jsonTools.jsonToList(portString, clazz);
  73. return portList;
  74. }
  75. // ==================================删除=====================================================
  76. /**
  77. * delete方式提交 接收 路径(路径传参) 成功返回(return "success") 失败返回(return "false")
  78. *
  79. * @throws IOException
  80. * @throws ClientProtocolException
  81. */
  82. public String delete(String url) throws Exception {
  83. // 创建HttpClient对象
  84. CloseableHttpClient httpclient = HttpClients.createDefault();
  85. // 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
  86. HttpDelete httpdelete = new HttpDelete(url);
  87. System.out.println(url);
  88. // 发送HttpDelete请求
  89. HttpResponse httpResponse = httpclient.execute(httpdelete);
  90. // 判断响应结果
  91. CloseableHttpResponse response = null;
  92. String jsonString = null;
  93. try {
  94. response = httpclient.execute(httpdelete);
  95. StatusLine status = response.getStatusLine();
  96. int state = status.getStatusCode();// 获取状态码 2、3开头成功 4、5错误
  97. // 具体网址https://blog.csdn.net/dufufd/article/details/53112184
  98. // 如果服务器成功地返回响应
  99. HttpEntity responseEntity = response.getEntity();
  100. jsonString = EntityUtils.toString(responseEntity);
  101. if (state == HttpStatus.SC_OK) {
  102. System.out.println("state" + state + "接收设置返回值" + jsonString);
  103. // 响应成功返回值
  104. return "success";
  105. } else {
  106. System.out.println("state" + state + "接收设置返回值" + jsonString);
  107. logger.error("请求返回:" + state + "(" + url + ")");
  108. // 响应失败返回值
  109. return "false";
  110. }
  111. } finally {
  112. if (response != null) {
  113. try {
  114. response.close();
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. try {
  120. httpclient.close();
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. }
  126.  
  127. // ==================================5.上传=====================================================
  128. /**
  129. * post方式提交(增,改) 传入 访问路径 参数 string(json) 成功返回(return "success") 失败返回(return
  130. * "false") https://blog.csdn.net/wangpeng047/article/details/19624529
  131. * https://blog.csdn.net/qq9808/article/details/78320816
  132. *
  133. * @throws IOException
  134. * @throws ClientProtocolException
  135. */
  136. public String post(String url, String params) throws ClientProtocolException, IOException {
  137. // 1. 创建HttpClient对象。
  138. CloseableHttpClient httpclient = HttpClients.createDefault();
  139. // 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
  140. HttpPost httpPost = new HttpPost(url);// 创建httpPost
  141. httpPost.setHeader("Accept", "application/json");
  142. httpPost.setHeader("Content-Type", "application/json");
  143. String charSet = "UTF-8";
  144. StringEntity entity = new StringEntity(params, charSet);
  145. // 3.如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams
  146. // params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
  147. httpPost.setEntity(entity);
  148. CloseableHttpResponse response = null;
  149. String jsonString = null;
  150. try {
  151. // 4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
  152. response = httpclient.execute(httpPost);
  153. // 5. 调用HttpResponse的getAllHeaders()、getHeaders(String
  154. // name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
  155. StatusLine status = response.getStatusLine();
  156. int state = status.getStatusCode();
  157. System.out.println("state=" + state);
  158. if (state == HttpStatus.SC_OK) {
  159. HttpEntity responseEntity = response.getEntity();
  160. jsonString = EntityUtils.toString(responseEntity);
  161. System.out.println("state" + state + "接收设置返回值" + jsonString);
  162. // 响应成功返回值
  163. return "success";
  164. } else {
  165. System.out.println("state" + state + "接收设置返回值" + jsonString);
  166. logger.error("请求返回:" + state + "(" + url + ")");
  167. // 响应失败返回值
  168. return "false";
  169. }
  170. // 6. 释放连接。无论执行方法是否成功,都必须释放连接
  171. } finally {
  172. if (response != null) {
  173. try {
  174. response.close();
  175. } catch (IOException e) {
  176. e.printStackTrace();
  177. }
  178. }
  179. try {
  180. httpclient.close();
  181. } catch (IOException e) {
  182. e.printStackTrace();
  183. }
  184. }
  185. }
  186.  
  187. }

  

//===================================================

<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<!-- https://mvnrepository.com/artifact/axis/axis -->
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>

webservice客户端 get delete post 请求的更多相关文章

  1. C#WebService 客户端通过Http调用请求(转)

    1.webservice帮助类 public class WebServiceHelper    {               public static string CallServiceByG ...

  2. 最简单易懂的webService客户端之soap+xml请求

    代码准备: 1.网络上有提供一些免费的服务器测试地址,可以上这里找一找:https://my.oschina.net/CraneHe/blog/183471 2.我选择了一个翻译地址:http://w ...

  3. 使用GSoap开发WebService客户端与服务端

    Gsoap 编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现, 从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多. 用gsoap开发web service的大致思路 我 ...

  4. 让python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE等请求

    这两天在用python的bottle框架开发后台管理系统,接口约定使用RESTful风格请求,前端使用jquery ajax与接口进行交互,使用POST与GET请求时都正常,而Request Meth ...

  5. 让python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE等请求(新方法)

    通过上篇博文的方法处理后,进入代码调试后发现ajax获取不了服务器端返回的数据,度娘后发现原来AJAX的OPTIONS请求方式是状态类型查询,即向服务器提交信息后不返回任何信息,只将执行状态(200状 ...

  6. (三)使用CXF开发WebService客户端

    前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程: 首先建一个Maven项目,项目名字,WS_Client: 然后我们要用CXF给我们 ...

  7. WebService客户端几种实现方式

    1.jdk原生调用(需要获取服务接口文件) import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Ser ...

  8. Eclipse内嵌的webservice客户端

    概述 Eclipse内嵌的webservice客户端,可用于发起请求,查看结果,展示请求和响应的报文. 详情 在Java EE视图,可以看到内嵌的webservice客户端浏览器登陆按钮 点击打开浏览 ...

  9. WebService Get/Post/Soap 方式请求

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.InputStream; im ...

随机推荐

  1. python-性能测试

    目录: 1.timeit 1.1 在命令后调用timeit 1.2 在代码中使用 1.3 创建计时器实例,通过autorange获得循环次数 1.4 Wall时间和CPU时间 2.profile和cP ...

  2. ios-复制字符串到剪贴板

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = self.label.text;

  3. spring 之 factory-bean & factory-method

    这两者常常是一起出现的,或者说他们经常是一起被使用的.但是其实是分为了两种情况: 1 同时使用factory-bean 和 factory-method 如果,我们在一个bean 元素上同时配置 fa ...

  4. ant 小结

    ant 的配置文件是xml 格式的. 其xml根元素是 project project元素下面有 property path env target filelist patternset 其中 tar ...

  5. 1. easyui tree 初始化的两种方式

    /** * 查询角色分类 */function queryRoleCategoryTree(selectId) { var url = basePath + 'rest/roleCategoryCon ...

  6. Android将Log写入文件

    为什么要将Log写入文件 运行应用程序的时候,大多数是不会连接着IDE的: 而当应用程序崩溃时,我们需要收集复现步骤,在设备上复现,并进行Debug: 而由于Android手机的多样性,有些问题是某个 ...

  7. scrapy之parallel

    Limiting Parallelism jcalderone May 22nd, 2006 This blog has moved! Read this post and its comments ...

  8. 【ASP.NET 插件】分享一款富文本web编辑器UEditor

    UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... <%@ Page Language ...

  9. leetcode153

    class Solution: def findMin(self, nums: 'List[int]') -> int: l = 0 h = len(nums)-1 while l < h ...

  10. 火狐Firefox浏览器所有历史版本下载地址

    Mozilla Firefox 频繁的更新,导致许多好用的插件在更新后不能兼容,而且想换回低版本还不容易啊,官网上只看到最新版本和前一个版本的下载. 这里为大家提供了一个下载链接,是来自Mozilla ...