java 调用 api接口
/*
* Copyright 2018 textile.com All right reserved. This software is the
* confidential and proprietary information of textile.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with textile.com.
*/
package com.bxm.advertisercms; import java.io.DataInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map; /**
* @fileName com.bxm.advertisercmsaaa.java
* @CopyRright (c) 2017-bxm:杭州微财科技有限公司
* @date 2018年3月9日 上午10:40:36
* @author chzq
*/
public class Test { /**
* @date 2018年3月9日 上午10:40:40
* @param args
* @author chzq
*/
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(
"https://buy.bianxianmao.com/shop/countInfo");
Map<String, String> params = new HashMap<String, String>();
params.put("bxm_id", "前端传过来的bxm_de的值");
params.put("status","");//固定值
params.put("modeltype", "");//固定值
String result1 = GetPostUrl(sb.toString(), params, "GET",null, , );
System.out.println(result1); } public static String GetPostUrl(String sendUrl, Map<String, String> params, String sendType, String charset,
int repeat_request_count, int repeat_request_max_count) {
URL url = null;
HttpURLConnection httpurlconnection = null; try {
// 构建请求参数
StringBuffer paramSb = new StringBuffer();
if (params != null) {
for (java.util.Map.Entry<String, String> e : params.entrySet()) {
paramSb.append(e.getKey());
paramSb.append("=");
// 将参数值urlEncode编码,防止传递中乱码
paramSb.append(URLEncoder.encode(e.getValue(), "UTF-8"));
paramSb.append("&");
}
paramSb.substring(, paramSb.length() - );
}
url = new URL(sendUrl + "?" + paramSb.toString());
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestMethod("GET");
httpurlconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpurlconnection.setDoInput(true);
httpurlconnection.setDoOutput(true); // 设置http请求超时时间30000毫秒(30秒)
httpurlconnection.setConnectTimeout();
httpurlconnection.setReadTimeout();
httpurlconnection.setUseCaches(true);
/*
* if (submitMethod.equalsIgnoreCase("POST")) {
* httpurlconnection.getOutputStream().write(postData.getBytes("GBK"
* )); httpurlconnection.getOutputStream().flush();
* httpurlconnection.getOutputStream().close(); }
*/ int code = httpurlconnection.getResponseCode();
if (code == ) {
DataInputStream in = new DataInputStream(httpurlconnection.getInputStream());
int len = in.available();
byte[] by = new byte[len];
in.readFully(by);
String rev = new String(by, "UTF-8"); in.close(); return rev;
} else {
// http 请求返回非 200状态时处理
return "<?xml version=\"1.0\" encoding=\"utf-8\" ?><error>发送第三方请求失败</error>";
} } catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null) {
httpurlconnection.disconnect();
}
}
return null;
} }
java 调用 api接口的更多相关文章
- Java 调用http接口(基于OkHttp的Http工具类方法示例)
目录 Java 调用http接口(基于OkHttp的Http工具类方法示例) OkHttp3 MAVEN依赖 Http get操作示例 Http Post操作示例 Http 超时控制 工具类示例 Ja ...
- Java调用webservice接口方法
java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ...
- C#使用windows服务定时调用api接口
使用VS创建windows服务项目: 创建好项目 会出现一个设计界面 右键弹出对话框 选择添加安装程序 名字什么的自己可以改: 项目目录: 打开项目中的ProjectInstaller.Design ...
- Python调用API接口的几种方式 数据库 脚本
Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...
- Python调用API接口的几种方式
Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...
- 调用API接口,查询手机号码归属地(3)
从mysql数据库获取电话号码,查询归属地并插入到数据库 #!/usr/bin/python # -*- coding: utf-8 -*- import json, urllib, sys, pym ...
- 调用API接口,查询手机号码归属地(2)
使用pymysql pip install pymysql 创建mysql测试表 CREATE TABLE `userinfo` ( `id` int(20) NOT NULL AUTO_INCREM ...
- 调用API接口,查询手机号码归属地(1)
使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...
- (二)通过JAVA调用SAP接口 (增加一二级参数)
(二)通过JAVA调用SAP接口 (增加一二级参数) 一.建立sap连接 请参考我的上一篇博客 JAVA连接SAP 二.测试项目环境准备 在上一篇操作下已经建好的环境后,在上面的基础上新增类即可 三. ...
随机推荐
- 关于activity的一点总结(一)
关于activity的重点: 参考网址:https://blog.csdn.net/qq_26787115/article/details/52556842 一.activity生命周期. 二..启动 ...
- python bmp转jpg 且灰度图转彩色
今天在简书,上看到了一个 bmp转jpg的代码,便记录一下. # coding:utf-8 import os from PIL import Image # bmp 转换为jpg,灰度图转RGB d ...
- zip 格式解析
1.格式 https://pkware.cachefly.net/webdocs/APPNOTE/APPNOTE-6.2.0.txt 官方文档 https://blog.csdn.net/hp9103 ...
- day47 选择器优先级及嵌套关系
复习 1.前端: 网页, html + css + js 2.html三个组成部分:标签,指令和转义字符 标签: <>包裹, 以字母开头, 可以结合-|数字, 能被浏览器解析的标记 3.常 ...
- spring boot 1.x配置,不断完善中
spring boot是典型的约定大于配置,那么很有必要对在开发过程中这些配置做统一的添加记录,以免用到的时候到处搜索,网上的东西又良莠不齐. server.port=8880 微服务注册中心,yml ...
- Zabbix 各种报错信息和遇到的问题处理(持续总结更新~~~~~)
问题1:Zabbix poller processes more than 75% busy 解决: 1.修改配置文件: # vim /etc/zabbix/zabbix_server.conf St ...
- paloalto防火墙接口使用方法及实例
1.获取账号相关的唯一API KEY windows:https://x.x.x.x/api/?type=keygen&user=username&password=password ...
- 转载:如何优雅的实现INotifyPropertyChanged接口
转载来源:http://www.cnblogs.com/TianFang/p/6240933.html 如何优雅的实现INotifyPropertyChanged接口 INotifyPropertyC ...
- 如何将一段文本编译成C#内存程序的过程
string code = null; // 1. 生成要编译的代码.(示例为了简单直接从程序集内的资源中读取) Stream stram = typeof(CodeDOM).Assembly .Ge ...
- span的title标签中的换行
var strs = data.flowSummary; strs=strs.replace(/燮r燮n/g," "); js的全局替换用/要替换的字符串/g span的titl ...