生产中遇到过这种问题,记录下java的接口调用问题。

一种是json方式;

 public static String sendPost(String url, JSONObject obj)throws IOException{
OutputStreamWriter out = null;
BufferedReader reader = null;
String response=""; /* String paramStr = "";
if (null != param) { //遍历参数Map添加到集合中
Set<String> keySet = param.keySet();
for (String key : keySet) {
paramStr = paramStr + key + ":" + param.get(key) + "&";
}
}
int lastAndIndex = paramStr.lastIndexOf("&");
if (lastAndIndex != -1) {
paramStr = paramStr.substring(0, lastAndIndex);
} System.out.println("地址===" + url);
System.out.println("入参===" + paramStr);*/ try {
URL httpUrl = null; //HTTP URL类 用这个类来创建连接
//创建URL
httpUrl = new URL(url);
//建立连接
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestProperty("accept", "*/*");// 设置通用的请求属性
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "keep-alive");
conn.setUseCaches(false);//设置不要缓存
conn.setInstanceFollowRedirects(true);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
//POST请求
out = new OutputStreamWriter(
conn.getOutputStream());
out.write(obj.toJSONString());
out.flush();
//读取响应
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
response+=lines;
}
reader.close();
// 断开连接
conn.disconnect();
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(reader!=null){
reader.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
} return response;
}

再来一个普通的str 调用接口;

    /**
* 向指定 URL 发送POST方法的请求
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPostWebReq(String url, Map<String, Object> param,int i) throws Exception {
String paramStr = "";
if (null != param) { //遍历参数Map添加到集合中
Set<String> keySet = param.keySet();
for (String key : keySet) {
paramStr = paramStr + key + "=" + param.get(key) + "&";
}
}
int lastAndIndex = paramStr.lastIndexOf("&");
if (lastAndIndex != -1) {
paramStr = paramStr.substring(0, lastAndIndex);
} System.out.println("地址===" + url);
System.out.println("入参===" + paramStr); PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();// 打开和URL之间的连接
conn.setRequestProperty("accept", "*/*");// 设置通用的请求属性
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setDoOutput(true);// 发送POST请求必须设置如下两行
conn.setDoInput(true);// 获取URLConnection对象对应的输出流
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));// 发送请求参数
out.print(paramStr);// flush输出流的缓冲
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));// 定义BufferedReader输入流来读取URL的响应
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
throw e;
} finally { // 使用finally块来关闭输出流、输入流
try {
if (in != null) in.close();
} catch (IOException ex) { }
if (out != null) out.close();
}
return result;
}

java 接口调用的更多相关文章

  1. OpenCV4Android开发之旅(一)----OpenCV2.4简介及 app通过Java接口调用OpenCV的示例

    转自:  http://blog.csdn.net/yanzi1225627/article/details/16917961 开发环境:windows+ADT Bundle+CDT+OpenCV-2 ...

  2. 十分钟搭建redis单机版 & java接口调用

    本次单机版redis服务器搭建采用的包为redis-3.0.0.tar.gz,主要是记录下安装的心得,不喜勿喷! 一.搭建redis服务器单机版 1.上传redis-3.0.0.tar.gz到服务器上 ...

  3. java接口调用——webservice就是一个RPC而已

    很多新手一听到接口就蒙逼,不知道接口是什么!其实接口就是RPC,通过远程访问别的程序提供的方法,然后获得该方法执行的接口,而不需要在本地执行该方法.就是本地方法调用的升级版而已,我明天会上一篇如何通过 ...

  4. 百度鹰眼Java接口调用增删改查实例

    因感觉百度鹰眼的使用场景比较符合实际业务,于是对百度鹰眼做了简单功能调试.刚开始使用springframework封装的RestTemplate,但是测试提示ak参数不存在.后又试了几种方法,均提示a ...

  5. Java接口调用工具类

    package com.qiyuan.util; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...

  6. 基于JAVA的全国天气预报接口调用示例

    step1:选择本文所示例的接口"全国天气预报接口" url:https://www.juhe.cn/docs/api/id/39/aid/87step2:每个接口都需要传入一个参 ...

  7. bugzilla4的xmlrpc接口api调用实现分享: xmlrpc + https + cookies + httpclient +bugzilla + java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能

    xmlrpc .  https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很 ...

  8. java后台调用HttpURLConnection类模拟浏览器请求(一般用于接口调用)

    项目开发中难免遇到外部接口的调用,小生今天初次接触该类,跟着API方法走了一遍,如有不对的地方,还请哆哆指正,拜谢! 1 package com.cplatform.movie.back.test; ...

  9. C#调用java接口报“Fault occurred while processing”异常问题

    服务在通常项目中总是要用到的C#的webservice.wcf,还有第三方的,比如java服务.一般来说调用都不会有什么问题,因为服务的标准都是一样的.要注意的就是:1.参数个数匹配:2.参数类型和返 ...

随机推荐

  1. Flyod 算法(两两之间的最短路径)

    Flyod 算法(两两之间的最短路径)动态规划方法,通过相邻矩阵, 然后把最后的结果存在这么一个矩阵里面,(i,j), #include <iostream> #include <v ...

  2. 标头“Vary:Accept-Encoding”指定方法[转]

    现在的新浏览器都支持压缩了,因此如果网站启用了GZip,可以无需再指定“Vary: Accept-Encoding”标头,不过指定“Vary: Accept-Encoding”标头会有更高的保险,而它 ...

  3. OpenXml读取word内容注意事项

    OpenXml读取word内容注意事项 1.使用OpenXml读取word内容,word后缀必须是".docx":如果word后缀是".doc"需要转成&quo ...

  4. Unix环境高级编程(十九)终端I/O

    终端I/O应用很广泛,用于终端.计算机之间的直接连线.调制解调器以及打印机等等.终端I/O有两种不同的工作模式: (1)规范模式输入处理:终端输入以行为单位进行处理,对于每个读要求,终端驱动程序最多返 ...

  5. ios学习之旅--oc对象的关系

    1.匿名对象:就是没有名字对象     1.匿名对象仅用一次     使用场景:     1.当我们仅仅要调用一个对象的某个方法一次的时候能够使用匿名对象 2.匿名对象能够作为函数的实际參数 #imp ...

  6. 解决电脑需要切换IP带来的MySQL连接问题

    直接上代码: import socket #获取本机电脑名 myname = socket.getfqdn(socket.gethostname( )) #获取本机ip myip = socket.g ...

  7. sumatrapdf 软件介绍

    sumatrapdf  软件介绍 介绍 支持PDF, ePub, Mobi, XPS, DjVu, CHM, CBZ 和 CBR格式的电子文件 仅支持Windows,原因见官网论坛 绿色版,小巧,于此 ...

  8. Java 异常处理的 9 个最佳实践

    在 Java 中,异常处理是个很麻烦的事情.初学者觉得它很难理解,甚至是经验丰富的开发者也要花费很长时间决定异常是要处理掉和抛出. 所以很多开发团队约定一些原则处理异常.如果你是一个团队的新成员,你可 ...

  9. haproxy有关session的问题

    在实验的时候遇到一个问题就是当我登录网站的时候,然后我再刷新一下,用户的状态就退出了 我现在的框架是这样的,前面有一台haproxy作为反向代理,后面有两台服务器跑的是java应用.后面两台服务器做的 ...

  10. UVA - 11609 Teams (排列组合数公式)

    In a galaxy far far awaythere is an ancient game played among the planets. The specialty of the game ...