package com.cn.eport.util.common;

import java.io.IOException;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.client.RestTemplate;
import com.cn.eport.service.Web;
public class webServiceUtil {
static Logger logger = Logger.getLogger(Web.class); /**
* get请求
*
* @return
*/
public static String doGet(String url) {
try {
HttpClient client = new DefaultHttpClient();
// 发送get请求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request); /** 请求发送成功,并得到响应 **/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/** 读取服务器返回过来的json字符串数据 **/
String strResult = EntityUtils.toString(response.getEntity()); return strResult;
}
} catch (IOException e) {
e.printStackTrace();
} return null;
}
/**
* 相当get请求
*
* @return
*/
public List<T> getPortList() {
// TODO Auto-generated method stub
RestTemplate client = new RestTemplate();
String url = "http://12.123.12.123:8080/abc/abcd";
String portString = client.getForObject(url, String.class);
JsonTools jsonTools = new JsonTools();
String className = "com.cn.eport.pojo.Port";
Class clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<T> portList = jsonTools.jsonToList(portString, clazz);
return portList;
}
// ==================================删除=====================================================
/**
* delete方式提交 接收 路径(路径传参) 成功返回(return "success") 失败返回(return "false")
*
* @throws IOException
* @throws ClientProtocolException
*/
public String delete(String url) throws Exception {
// 创建HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
HttpDelete httpdelete = new HttpDelete(url);
System.out.println(url);
// 发送HttpDelete请求
HttpResponse httpResponse = httpclient.execute(httpdelete);
// 判断响应结果
CloseableHttpResponse response = null;
String jsonString = null;
try {
response = httpclient.execute(httpdelete);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();// 获取状态码 2、3开头成功 4、5错误
// 具体网址https://blog.csdn.net/dufufd/article/details/53112184
// 如果服务器成功地返回响应
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
if (state == HttpStatus.SC_OK) {
System.out.println("state" + state + "接收设置返回值" + jsonString);
// 响应成功返回值
return "success";
} else {
System.out.println("state" + state + "接收设置返回值" + jsonString);
logger.error("请求返回:" + state + "(" + url + ")");
// 响应失败返回值
return "false";
}
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // ==================================5.上传=====================================================
/**
* post方式提交(增,改) 传入 访问路径 参数 string(json) 成功返回(return "success") 失败返回(return
* "false") https://blog.csdn.net/wangpeng047/article/details/19624529
* https://blog.csdn.net/qq9808/article/details/78320816
*
* @throws IOException
* @throws ClientProtocolException
*/
public String post(String url, String params) throws ClientProtocolException, IOException {
// 1. 创建HttpClient对象。
CloseableHttpClient httpclient = HttpClients.createDefault();
// 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
HttpPost httpPost = new HttpPost(url);// 创建httpPost
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
String charSet = "UTF-8";
StringEntity entity = new StringEntity(params, charSet);
// 3.如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams
// params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
String jsonString = null;
try {
// 4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
response = httpclient.execute(httpPost);
// 5. 调用HttpResponse的getAllHeaders()、getHeaders(String
// name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
System.out.println("state=" + state);
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
System.out.println("state" + state + "接收设置返回值" + jsonString);
// 响应成功返回值
return "success";
} else {
System.out.println("state" + state + "接收设置返回值" + jsonString);
logger.error("请求返回:" + state + "(" + url + ")");
// 响应失败返回值
return "false";
}
// 6. 释放连接。无论执行方法是否成功,都必须释放连接
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

  

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

<!-- 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. 使用DolphinPHP的框架中的excel插件导入数据

    直接上函数吧 public function importfile() { if ($this->request->isPost()) { if($_POST['files']) { Cu ...

  2. 《算法》第四章部分程序 part 9

    ▶ 书中第四章部分程序,包括在加上自己补充的代码,两种拓扑排序的方法 ● 拓扑排序 1 package package01; import edu.princeton.cs.algs4.Digraph ...

  3. spark 常用技巧总结

    解析url scala> import java.net.URLimport java.net.URL scala> val urlstr="http://www.baidu.c ...

  4. PyCharm 安装使用

    服务器激活地址(转载)http://www.cnblogs.com/littlehb/p/7784517.html   PyCharm 服务器激活地址: 最近用edu邮箱申请了一个JetBrains针 ...

  5. 转载:官方Caffe-windows 配置与示例运行

    转载来自:http://blog.csdn.net/guoyk1990/article/details/52909864 本文主要介绍官方给出的caffe-windows的配置及如何训练mnist数据 ...

  6. react-native android打包

    看了官网测试的是可以的,自己整理下,方便后面查看 先是生产安卓证书,安卓证书生成,点这里.这里掠过 生成安卓证书,记住2个密码 秘钥库口令 和 私钥密码 1.然后把你生成的安卓证书放到文件放到你工程中 ...

  7. Valgrind简单用法 (转)

    转自 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian Seward刚获得了今年的G ...

  8. jsfl 常用自定义方法

    //创建文件夹 function creatFile(fileURl) { if (FLfile.createFolder(fileURl)) { //alert("创建成功 "+ ...

  9. php中显示数组与对象的实现代码

    1. 使用 print_r ( $array/$var ) print 是打印的意思,而r则取自Array的单词,那么该函数的功能就是打印数组内容,它既可以打印数组内容,也可以打印普通的变量. pri ...

  10. C语言复习:结构体

    结构体专题 01.结构体类型定义及结构体变量定义     char c1,char c2, char name[62]; int age     char name[62]; int age,char ...