使用HttpClient 依赖jar包

1:commons-httpclient-3.0.jar

2:commons-logging-1.1.1.jar

3:commons-codec-1.6.jar

本地调用测试===>>>>>>>>>>>>


package com.me.cn.siteTrans.test;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import com.google.gson.Gson; public class SiteTran {
public static void main(String[] args) {
String result = httpClientTest();
System.out.println(result);
}
public static String httpClientTest(){
HttpClient client = new HttpClient() ;
PostMethod method = null;//post 方式 get 方式 GetMethod gMethod
String result = "" ;
try {
method = new PostMethod("http://localhost:8666/test/siteTrans/returnJson.do") ;
method.setParameter("param","test");//设置参数
client.executeMethod(method);
if(method.getStatusCode() == HttpStatus.SC_OK){//响应成功
result = method.getResponseBodyAsString();//调用返回结果
}else{//不成功组装结果
Map<String , Object >map = new HashMap<String , Object>();
Gson gson = new Gson() ;
map.put("success", false);
result = gson.toJson(map);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
method.releaseConnection();
}
return result ;
}
}

 

模拟第三方接口===>>>>>>>>>>>>

package com.cn.me.siteTrans.interface;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson; @Controller
@RequestMapping("/siteTrans")
@SuppressWarnings("all")
public class SiteTransCtrl {
@RequestMapping("/returnJson")
@ResponseBody
public Map returnJson(String param){
Map<String , Object> map = new HashMap<String , Object>() ;
try {
System.out.println(param);
map.put("success", true) ;
map.put("flag", true);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}

SpringMVC 结合HttpClient调用第三方接口实现的更多相关文章

  1. 使用HttpClient调用第三方接口

    最近项目中需要调用第三方的Http接口,这里我用到了HttpClient. 首先我们要搞明白第三方接口中需要我们传递哪些参数.数据,搞明白参数以后我们就可以使用HttpClient调用接口了. 1.调 ...

  2. Java调用第三方接口示范

    在项目开发中经常会遇到调用第三方接口的情况,比如说调用第三方的天气预报接口. 使用流程[1]准备工作:在项目的工具包下导入HttpClientUtil这个工具类,或者也可以使用Spring框架的res ...

  3. java springboot调用第三方接口 借助hutoool工具类 爬坑

    楼主是个后端小白一枚,之前没接触过后端,只学了java基本语法,还是在学校老师教的,学的很浅,什么ssh.ssm框架都没有学,最近在自学spring boot,看书学也看不是很懂,就在b站上看教学视频 ...

  4. Java之HttpClient调用WebService接口发送短信源码实战

    摘要 Java之HttpClient调用WebService接口发送短信源码实战 一:接口文档 二:WSDL 三:HttpClient方法 HttpClient方法一 HttpClient方法二 Ht ...

  5. springMVC、httpClient调用别人提供的接口!!!(外加定时调用)

    import com.ibm.db.util.AppConfig; import com.ibm.db.util.JacksonUitl; import org.apache.http.HttpEnt ...

  6. java代码调用第三方接口

    一.利用httpclient来字符串参数(url是第三方接口,不带参数,如:http://192.168.16.200:8081/faceInfo/list,param是url后面所要带的参数) pu ...

  7. Java调用第三方接口工具类(json、form)

    1.JSON值访问 /** * 调用对方接口方法 * @param path 对方或第三方提供的路径 * @param data 向对方或第三方发送的数据,大多数情况下给对方发送JSON数据让对方解析 ...

  8. 使用WebClient调用第三方接口

    需要调用一个第三方接口,传参返回数据 本来是很简单的一个需求,搞了一天没整好 首先在POSTMAN中测试没有问题,但是使用jquery ajax在前台就会涉及到跨域 虽然设置了 无论怎么写都会报错 C ...

  9. httpclient调用webservice接口的方法实例

    这几天在写webservice接口,其他的调用方式要生成客户端代码,比较麻烦,不够灵活,今天学习了一下httpclient调用ws的方式,感觉很实用,话不多说,上代码 http://testhcm.y ...

随机推荐

  1. Maven的包依赖冲突可引发java.lang.IncompatibleClassChangeError错误

    新版API上线后,发现LOG文件没有正常输出.查看Tomcat的Log文件发现如下的错误信息 May , :: AM com.sun.xml.ws.server.sei.EndpointMethodH ...

  2. 应用EF访问SQLite数据

    创建项目,应用EF访问SQLite 1.创建项目 项目结构初始结构如下图所示,Netage.Data.SQLite 类库项目用于定义访问数据的接口和方法,Netage.SQLiteTest.UI 控制 ...

  3. curl php 小记

    /* 开发用到. */ https 需加入: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查

  4. WCF之Binding详解

    本文的出发点: 通过阅读本文,您能了解以下知识: WCF中的Binding是什么? Binding的组 成? Binding Element 的分类? Binding描述 了那些层面的信息? 选择正确 ...

  5. 为控件Button设置快捷键(组合键)

    控件MenuStrip和ContextMenuStrip可通过ShortCcutKeys属性设置快捷键,而控件Button没有ShortcutKey属性,如何为控件Button设置快捷键呢(组合件键) ...

  6. poj1050

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39081   Accepted: 20639 Desc ...

  7. [ASP.NET]

    public static int iA; protected void Button1_Click(object sender, EventArgs e) { //Label1.Text Respo ...

  8. 用Java写算法之归并排序

    转自:http://flyingcat2013.blog.51cto.com/7061638/1281026 前面的三种排序算法(冒泡排序,选择排序,插入排序)在平均情况下均为O(n^2)复杂度,在处 ...

  9. 关于前端build工具

    第一.build工具的核心就是帮你安装和帮你做事.安装类工具:Npm.Bower.Yeoman等          做事类工具:Node.Grunt.gulp.Webpack等 安装类工具几乎什么东西 ...

  10. JS中简单原型的使用