接口调用类3

需要获取token的接口调用
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import org.junit.Test;

public class HttpBIS {

public String getPostMethod(String url, String parameter) throws Exception {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        try {
          RequestEntity entity = new StringRequestEntity(parameter.replaceAll("\\+", "%2B"), "application/json", "UTF-8");

post.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded;charset=UTF-8");
          post.setRequestHeader("Authorization", "Basic 密码");
          post.setRequestEntity(entity);

client.executeMethod(post);

System.out.println(post.getStatusLine());
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }

String response = new String(post.getResponseBodyAsString().getBytes( "UTF-8"));

post.releaseConnection();
        return response;
      }

public static String getPostTokenMethod(String url, String parameter)
        throws Exception
      {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        try {
          RequestEntity entity = new StringRequestEntity(parameter.replaceAll("\\+", "%2B"), "application/json", "UTF-8");

post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
          post.setRequestHeader("Authorization", "Basic 密码");
          post.setRequestEntity(entity);

client.executeMethod(post);

System.out.println("token调用状态:" + post.getStatusLine());
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }

String response = new String(post.getResponseBodyAsString().getBytes("UTF-8"));

post.releaseConnection();
        return response;
      }

public static String getPostMethod(String url, String parameter, String header)
        throws Exception
      {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        try {
          RequestEntity entity = new StringRequestEntity(parameter.replaceAll("\\+", "%2B"), "application/json", "UTF-8");

post.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
          post.setRequestHeader("Authorization", header);
          post.setRequestEntity(entity);

client.executeMethod(post);

System.out.println("GDZCtoBIS状态:" + post.getStatusLine() + ",头部编码:" + post.getRequestCharSet());
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }

String response = new String(post.getResponseBodyAsString().getBytes("UTF-8"), "UTF-8");
        new String(post.getResponseBodyAsString());

post.releaseConnection();
        return response;
      }

public void test2()
      {
        try
        {
          String b = getGetMethod("https://10.2.1.193/api/departments/72");
          System.out.println("部门信息返回值:" + b);
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
        
}

原文地址:https://www.cnblogs.com/cjxns/p/11125737.html

时间: 2024-08-02 12:54:43

接口调用类3的相关文章

接口调用类-FenGKun

/** * 接口调用类 * * @author FenGKun * */ public class DataUtil { private static final String TAG = "DataUtil.java"; private static final boolean isDebug = true; /* ****** 课程[Begin] ****** */ /** 课程:字 */ public static final int CATE_WORD = 1; /** 课程:

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

xmlrpc .  https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很少,针对xmlrpc的有但是基本都是http协议的,https下的认证处理比较麻烦,而且会话保持也是基本没有太多共享,所以本人决定结合xmlrpc\bugzilla官方文档,网友文章,结合个人经验总结而成,已经在window2007 64+jdk7位机器上调试通过 手把手教你如何实现: 第一步: 在

基于Axis1.4的webservice接口开发(接口调用)

一.webservice接口代码参考上一篇博客: http://www.cnblogs.com/zhukunqiang/p/7125668.html 二.代码部分 1.新建Web Project工程,取名为axiswebserviceClint,并导入Axis1.4所需jar包(将jar包粘贴到lib目录). 2.新建webservice接口调用类: package com.no8.clint; import java.net.URL; import javax.xml.rpc.ServiceE

从头认识java-12.6 接口与类型信息(怎么绕过接口直接调用类的所有方法)

这一章节我们来讨论一下接口与类型信息. 在之前的章节里面我们提到接口,父类引用子类对象,然后把方法给缩窄了,但是我们这一章节学习到反射,这个约束就变得没有那么严格. 我们来看看下面的例子: package com.ray.ch11; public class Test { public static void main(String[] args) { Person man = new Man(); man.sleep(); man.run(); // man.say();//error } }

通过接口调用接口实现类实现松耦合(接口的多态)

项目开发在各个层中通过接口实现松耦合的时候,如何实现接口调用接口实现类? 这里的要求是接口有多个实现类,通过接口调用不的接口实现类! 开始,我想到了IOC(IOC最近忒火),确实有很多开发好的类库unity,ninject等等,但是这些类库都有点太强大了,例 如unity 是可以很容易实现我的要求,那么通过RegisterType<I, N>>(String name)注册:通过Resolve<I>(name) 就可以 用接口调用相应的接口实现类,如果想了解一下unity,

Spring-Boot ? ShapeFile文件读写工具类+接口调用

一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path":"c:/test", "geom":"MULTIPOLYGON(((101.870371 25.19228,101.873633 25.188183,101.880564 25.184416,101.886808 25.186028,101.89204

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

项目开发中难免遇到外部接口的调用,小生今天初次接触该类,跟着API方法走了一遍,如有不对的地方,还请哆哆指正,拜谢! 1 package com.cplatform.movie.back.test; 2 3 import java.io.BufferedReader; 4 import java.io.DataOutputStream; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 impor

ThinkPHP整合支付宝即时到账接口调用

首先是在支付宝的蚂蚁金服开放平台下载PHP的demo: 1 https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1 下载地址如上,里面有个MD5加密方式UTF8的PHP的demo可以拿来用,可以直接在localhost上跑,传入合适的参数即使体验支付一次,这点和微信支付接口调用的demo相同. 现在我们尝试将其中demo的lib类文件整合到thinkphp之中去. 将四个lib改名放入Vend

短信接口调用——阿里大于API开发心得

互联网上有许多公司提供短信接口服务,诸如网易云信.阿里大于等等.我在自己项目里需要使用到短信服务起到通知作用,实际开发周期三天,完成配置.开发和使用,总的说,阿里大于提供的接口易于开发,非常的方便,短信费用是计数缴纳的,作为个人开发者,我使用的服务产生的费用为0.45¥/条(10万条以下). 现在要实现一个例会短信群发通知的功能,所有被通知对象信息均存于Mysql中,应用架构采用asp.net MVC .首先准备好获取的API各项(以下各项服务参数都需要在大于官网上申请), 申请好自己的短信签名