/*
* 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接口的更多相关文章

  1. Java 调用http接口(基于OkHttp的Http工具类方法示例)

    目录 Java 调用http接口(基于OkHttp的Http工具类方法示例) OkHttp3 MAVEN依赖 Http get操作示例 Http Post操作示例 Http 超时控制 工具类示例 Ja ...

  2. Java调用webservice接口方法

                             java调用webservice接口   webservice的 发布一般都是使用WSDL(web service descriptive langu ...

  3. C#使用windows服务定时调用api接口

    使用VS创建windows服务项目: 创建好项目  会出现一个设计界面 右键弹出对话框 选择添加安装程序 名字什么的自己可以改: 项目目录: 打开项目中的ProjectInstaller.Design ...

  4. Python调用API接口的几种方式 数据库 脚本

    Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...

  5. Python调用API接口的几种方式

    Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...

  6. 调用API接口,查询手机号码归属地(3)

    从mysql数据库获取电话号码,查询归属地并插入到数据库 #!/usr/bin/python # -*- coding: utf-8 -*- import json, urllib, sys, pym ...

  7. 调用API接口,查询手机号码归属地(2)

    使用pymysql pip install pymysql 创建mysql测试表 CREATE TABLE `userinfo` ( `id` int(20) NOT NULL AUTO_INCREM ...

  8. 调用API接口,查询手机号码归属地(1)

    使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...

  9. (二)通过JAVA调用SAP接口 (增加一二级参数)

    (二)通过JAVA调用SAP接口 (增加一二级参数) 一.建立sap连接 请参考我的上一篇博客 JAVA连接SAP 二.测试项目环境准备 在上一篇操作下已经建好的环境后,在上面的基础上新增类即可 三. ...

随机推荐

  1. python day04笔记总结

    2019.4.1 S21 day04笔记总结 昨日内容补充 1.解释器/编译器 1.解释型语言.编译型语言 2.解释型:写完代码后提交给解释器,解释器将代码一行行执行.(边接收边解释/实时解释) 常用 ...

  2. 开源虚拟化KVM(二)管理虚拟存储

    五,管理虚拟存储 5.1 虚拟磁盘概述 5.1.1 虚拟化项目中存储的注意事项 [x] 存储的性能几乎总是虚拟化的瓶颈 [x] 通过多个硬盘驱动以分布磁盘I/O来实现存储解决方案 [x] 考虑部署集中 ...

  3. python经典案例

    前言:初学者对python的流程语句有一定的了解,但是运用起来总会磕磕碰碰.本文总结了一些初学者在学习python时做的经典案例 一.名片管理系统(限单个名片) info = {'name':'jam ...

  4. 如何用frp进行来无影去无踪

    准备工作 和 注意事项: 1.frp 下载地址  https://github.com/fatedier/frp/releases 2. 需要给有公网ip 的服务端服务器 和 本地客户端服务器 各放一 ...

  5. mongo中用嵌套结构优势是什么

    首先需要知道,MongoDB是NoSQL中的一种,是不直接支持Join的,这是NoSQL的一个特点,不需要直接支持Join,可以将横向扩展以及性能做到更好. 但是这不等于说MongoDB不能做Join ...

  6. Python设计模式 - UML - 定时图(Timing Diagram)

    简介 定时图也是一种交互图,用来描述对象或实体随时间变化的状态或值,及其相应的时间或期限约束.定时图应用较广,并不局限于软件工程领域. 定时图侧重与时间线相关的值或状态的改变,这些改变可能来自于收到消 ...

  7. python的requests模块参数详解

    import requests print(dir(requests)) # 1.方法 # ['ConnectTimeout', 'ConnectionError', 'DependencyWarni ...

  8. strin 数组转换成int 数组

    string[] strarry = ids.Trim(',').Split(','); int[] arryInts = Array.ConvertAll<string, int>(st ...

  9. Git那些事儿

    Git是目前世界上最先进的分布式版本控制系统,适合多人协作开发的大型项目.我平常也经常使用git,来管理自己的几个小项目.简单说说git的原理和git的特点!(只有知道了一个工具的运行原理,设计思路, ...

  10. bhttpd

    以前产品应用是用串口做控制台,写了一个带简单命令历史和命令补全功能的控制台Shell,用作程序的调试,包括查看系统状态和调试修改设定等等.确实非常好用,对很多现场简单问题的快速定位起到了很好的作用.系 ...