/*
* 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来袭,教你用Neo4j构建“复联4”人物关系图谱!

    来源商业新知网,原标题:Python来袭,教你用Neo4j构建“复联4”人物关系图谱!没有剧透! 复仇者联盟 之绝对不剧透 漫威英雄们为了不让自己剧透也是使出了浑身解数.在洛杉矶全球首映礼上记者费尽心 ...

  2. C++/CLI

    [C++/CLI] A C++/CLI application or component uses extensions to C++ syntax (as allowed by the C++ Sp ...

  3. cdn帮助链接汇集

    1. 如何查看节点和站点的流量,负载和连接信息 2. cdnbest常见状态码解释 3. 如何让用户访问走最近最快的线路(分组线路) 4. cdnbest里如何查看网站是否被缓存 5. cdnbest ...

  4. kettle学习笔记及实践

    转自萤火的萤 最近在用kettle迁移数据,从对kettle一点不会到比较熟悉,对于期间的一些问题和坑做了记录和总结,内容涵盖了使用的经验和技巧,踩到的坑.最佳实践和优化前后结果对比. 常用转换组件 ...

  5. Selenium+TestNG+Maven(2)

    转载自http://www.cnblogs.com/hustar0102/p/5885115.html selenium介绍和环境搭建 一.简单介绍 1.selenium:Selenium是一个用于W ...

  6. 关于逻辑回归是否线性?sigmoid

    from :https://www.zhihu.com/question/29385169/answer/44177582 逻辑回归的模型引入了sigmoid函数映射,是非线性模型,但本质上又是一个线 ...

  7. Ambari2.6.2 HDP2.6.5 大数据集群搭建

    Ambari 2.6.2 中 HDFS-2.7.3 YARN-2.7.3 HIVE-1.2.1 HBASE-1.1.2 ZOOKEEPER-3.4.6 SPARK-2.3.0 注:本文基于root用户 ...

  8. pwnable.kr-random-witeup

    看源代码. 可知,在linux下生成个随机数在于输入数异或等于固定值即可,而且吧, 随机数是固定的. 先得出随机数. random=1804289383 OK,接下来用计算器异或就行啦.0xB526F ...

  9. idea连接mysql

    https://blog.csdn.net/Golden_soft/article/details/80952243

  10. sqlserver改主键初始ID