远程调用第三方接口

在实际的项目开发中,经常需要调用远程接口,下面给出一个例子

public static String doPost(String urlStr,String json,String charset) {
        String result = null;
        System.out.println("请求参数:"+json);
        try {
            //获取目标地址
            URL url = new URL(urlStr);
            //创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //设置向HttpURLConnection输出、输入(发送数据、接收数据),当请求为post时必须设置这两个参数
            connection.setDoOutput(true);
            connection.setDoInput(true);
            //设置请求方式
            connection.setRequestMethod("POST");
            //设置是否开启缓存,post请求时,缓存必须关掉
            connection.setUseCaches(false);

            //设置连接是否自动处理重定向(setFollowRedirects:所用http连接;setInstanceFollowRedirects:本次连接)
            connection.setInstanceFollowRedirects(true);
            //设置提交内容类型
            connection.setRequestProperty("Content-Type","application/json");
            connection.setRequestProperty("Host","172.16.24.63:8000");
            connection.setRequestProperty("Cookie", "=");
            connection.setRequestProperty("X-CSRF-TOKEN", "2572fgtdrged");
            //开始连接
            connection.connect();
            //发送请求
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(json.getBytes(charset));
            out.flush();
            out.close();
            //读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), charset));
            String lines;
            StringBuffer sb = new StringBuffer("");
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes());
                sb.append(lines);
            }
            result = sb.toString();
            System.out.println("请求返回结果:"+result);
            reader.close();
            // 断开连接
            connection.disconnect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

引用:https://blog.csdn.net/qq_45171942/article/details/105450631

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
版本: 5.0.0.0 (build 2014.07.07) 1. 概述 RCK(remote call kernel 缩写)为远程调用内核, 其通讯协议为自定义数据流协议。 RCK 负责远程调用过程中的数据组织, 并不关心实际物理通讯协议, 实则属于逻辑层通讯 协议。 RCK 通讯槽接口(RCKSlot)负责数据接收和发送, 属于通讯的适配层, 由第三方来实现 实际的数据传输接口。 RCK 包含 Application, Function, Connection, Command, Response 和 Fields 六 大类, 其主要功能如下: a. Application 类主要负责 Function 的组织和 Response 的分发执行; b. Function 类主要负责 Function 的定义及按各模式调用; c. Connection 类主要负责登录对端应用, Command 请求执行, Response 应答管理, 以及发送和接收数据等等; d. Command 类主要负责函数参数传入, 以及返回值和返回字段值读取; e. Response 类主要负责对端指定 Function 请求的执行处理, 如参数读取、返回 值编辑及提交; f. Fields 类主要负责数据库字段值及定义集的组包和拆包。 2. RCK 通讯槽接口定义 参见 <RCKSlot.h> 文件 3. RCK 接口定义 参见 <RCKernel.h> 文件 4. RC 的自环接口定义(注: 内部实现 RCK 通讯槽接口) 自环接口用于进程内通过 Command 调用 Application 函数, 便于输出接口统一。 参见 <RC4SL.h> 文件 5. RC 的 TCP 接口定义(注: 内部实现 RCK 通讯槽接口) 参见 <RC4TCP.h> 文件 6. RC 的共享内存通讯接口定义(注: 内部实现 RCK 通讯槽接口) 参见 <RC4SHM.h> 文件 RC 压缩包中含有如下文件: -------------------------------------------------------------------------------- \RC 5.0\_exports\Delphi\RC4SHM.pas \RC 5.0\_exports\Delphi\RC4SL.pas \RC 5.0\_exports\Delphi\RC4TCP.pas \RC 5.0\_exports\Delphi\RCKernel.pas \RC 5.0\_exports\Delphi\RCKSlot.pas \RC 5.0\_exports\VC++\RC4SHM.h \RC 5.0\_exports\VC++\RC4SHM32.lib \RC 5.0\_exports\VC++\RC4SHM64.lib \RC 5.0\_exports\VC++\RC4SL.h \RC 5.0\_exports\VC++\RC4SL32.lib \RC 5.0\_exports\VC++\RC4SL64.lib \RC 5.0\_exports\VC++\RC4TCP.h \RC 5.0\_exports\VC++\RC4TCP32.lib \RC 5.0\_exports\VC++\RC4TCP64.lib \RC 5.0\_exports\VC++\RCKernel.h \RC 5.0\_exports\VC++\RCKernel.lib \RC 5.0\_exports\VC++\RCKernel64.lib \RC 5.0\_exports\VC++\RCKSlot.h \RC 5.0\_rc32\RC4SHM32.dll \RC 5.0\_rc32\RC4SL32.dll \RC 5.0\_rc32\RC4TCP32.dll \RC 5.0\_rc32\RCKernel.dll \RC 5.0\_rc64\RC4SHM64.dll \RC 5.0\_rc64\RC4SL64.dll \RC 5.0\_rc64\RC4TCP64.dll \RC 5.0\_rc64\RCKernel.dll \RC 5.0\classes\RCK\Delphi\RCKernel.pas \RC 5.0\classes\RCK\Delphi\RCKObjs.pas \RC 5.0\classes\RCK\Delphi\RCKSlot.pas \RC 5.0\classes\RCK\VC++\RCKernel.h \RC 5.0\classes\RCK\VC++\RCKernel.lib \RC 5.0\classes\RCK\VC++\RCKernel64.lib \RC 5.0\classes\RCK\VC++\RCKObjs.cpp \RC 5.0\classes\RCK\VC++\RCKObjs.h \RC 5.0\classes\RCK\VC++\RCKSlot.h \RC 5.0\classes\SHM\Delphi\RC4MSrvObj.pas \RC 5.0\classes\SHM\Delphi\RC4SHM.pas \RC 5.0\classes\SHM\VC++\RC4MSrvObj .cpp \RC 5.0\classes\SHM\VC++\RC4MSrvObj .h \RC 5.0\classes\SHM\VC++\RC4SHM.h \RC 5.0\classes\SHM\VC++\RC4SHM32.lib \RC 5.0\classes\SHM\VC++\RC4SHM64.lib \RC 5.0\classes\TCP\Delphi\RC4TCP.pas \RC 5.0\classes\TCP\Delphi\RC4TSrvObj.pas \RC 5.0\classes\TCP\VC++\RC4TCP.h \RC 5.0\classes\TCP\VC++\RC4TCP32.lib \RC 5.0\classes\TCP\VC++\RC4TCP64.lib \RC 5.0\classes\TCP\VC++\RC4TSrvObj.cpp \RC 5.0\classes\TCP\VC++\RC4TSrvObj.h \RC 5.0\demo\__lib32\include\ (KYLib for VC6) \RC 5.0\demo\__lib32\KYLib.lib \RC 5.0\demo\__lib32\KYLib_d.lib \RC 5.0\demo\__lib32\RC4SHM32.lib \RC 5.0\demo\__lib32\RC4SL32.lib \RC 5.0\demo\__lib32\RC4TCP32.lib \RC 5.0\demo\__lib32\RCKernel.lib \RC 5.0\demo\__lib64\include\ (KYLib for VS2008) \RC 5.0\demo\__lib64\KYLib.lib \RC 5.0\demo\__lib64\KYLib_d.lib \RC 5.0\demo\__lib64\RC4SHM64.lib \RC 5.0\demo\__lib64\RC4SL64.lib \RC 5.0\demo\__lib64\RC4TCP64.lib \RC 5.0\demo\__lib64\RCKernel.lib \RC 5.0\demo\AppDemo\release\AppDemo.dll \RC 5.0\demo\AppDemo\release\AppDemo.lib \RC 5.0\demo\AppDemo\units\ADExports.cpp \RC 5.0\demo\AppDemo\units\ADExports.h \RC 5.0\demo\AppDemo\units\DemoApp.cpp \RC 5.0\demo\AppDemo\units\DemoApp.h \RC 5.0\demo\AppDemo\units\RCKernel.h \RC 5.0\demo\AppDemo\units\RCKObjs.cpp \RC 5.0\demo\AppDemo\units\RCKObjs.h \RC 5.0\demo\AppDemo\units\RCKSlot.h \RC 5.0\demo\AppDemo\AppDemo.cpp \RC 5.0\demo\AppDemo\AppDemo.def \RC 5.0\demo\AppDemo\AppDemo.dsp \RC 5.0\demo\AppDemo\AppDemo.dsw \RC 5.0\demo\AppDemo\AppDemo.rc \RC 5.0\demo\AppDemo\resource.h \RC 5.0\demo\TestRC\release\TestRC4SHM.exe \RC 5.0\demo\TestRC\release\TestRC4SL.exe \RC 5.0\demo\TestRC\release\TestRC4TCP.exe \RC 5.0\demo\TestRC\release64\TestRC4SHM.exe \RC 5.0\demo\TestRC\release64\TestRC4SL.exe \RC 5.0\demo\TestRC\release64\TestRC4TCP.exe \RC 5.0\demo\TestRC\units\DemoApp.cpp \RC 5.0\demo\TestRC\units\DemoApp.h \RC 5.0\demo\TestRC\units\RC4SHM.h \RC 5.0\demo\TestRC\units\RC4SL.h \RC 5.0\demo\TestRC\units\RC4TCP.h \RC 5.0\demo\TestRC\units\RCKernel.h \RC 5.0\demo\TestRC\units\RCKObjs.cpp \RC 5.0\demo\TestRC\units\RCKObjs.h \RC 5.0\demo\TestRC\units\RCKSlot.h \RC 5.0\demo\TestRC\units\TestRC4SHM.cpp \RC 5.0\demo\TestRC\units\TestRC4SL.cpp \RC 5.0\demo\TestRC\units\TestRC4TCP.cpp \RC 5.0\demo\TestRC\resource.h \RC 5.0\demo\TestRC\TestRC.dsw \RC 5.0\demo\TestRC\TestRC.sln \RC 5.0\demo\TestRC\TestRC4SHM.dsp \RC 5.0\demo\TestRC\TestRC4SHM.rc \RC 5.0\demo\TestRC\TestRC4SHM.vcproj \RC 5.0\demo\TestRC\TestRC4SL.dsp \RC 5.0\demo\TestRC\TestRC4SL.rc \RC 5.0\demo\TestRC\TestRC4SL.vcproj \RC 5.0\demo\TestRC\TestRC4TCP.dsp \RC 5.0\demo\TestRC\TestRC4TCP.rc \RC 5.0\demo\TestRC\TestRC4TCP.vcproj \RC 5.0\test\rc-tool\AppDemo.dll \RC 5.0\test\rc-tool\RC4SHM32.dll \RC 5.0\test\rc-tool\RC4SL32.dll \RC 5.0\test\rc-tool\RC4TCP32.dll \RC 5.0\test\rc-tool\RCKernel.dll \RC 5.0\test\rc-tool\RCTool.exe \RC 5.0\test\rc-tool\RCTool.ini \RC 5.0\test\rc-tool\RCTool.rtc \RC 5.0\test\rc-tool\RCTool.rte \RC 5.0\test\rc-tool\RCTool.rts \RC 5.0\test\test-rc4shm\RC4SHM32.dll \RC 5.0\test\test-rc4shm\RCKernel.dll \RC 5.0\test\test-rc4shm\TestRC4SHM.dat \RC 5.0\test\test-rc4shm\TestRC4SHM.exe \RC 5.0\test\test-rc4shm\TestRC4SHM.ini \RC 5.0\test\test-rc4shm\TestRC4SHM.txt \RC 5.0\test\test-rc4sl\RC4SL32.dll \RC 5.0\test\test-rc4sl\RCKernel.dll \RC 5.0\test\test-rc4sl\TestRC4SL.exe \RC 5.0\test\test-rc4sl\TestRC4SL.ini \RC 5.0\test\test-rc4sl\TestRC4SL.txt \RC 5.0\test\test-rc4tcp\RC4TCP32.dll \RC 5.0\test\test-rc4tcp\RCKernel.dll \RC 5.0\test\test-rc4tcp\TestRC4TCP.dat \RC 5.0\test\test-rc4tcp\TestRC4TCP.exe \RC 5.0\test\test-rc4tcp\TestRC4TCP.ini \RC 5.0\test\test-rc4tcp\TestRC4TCP.txt --------------------------------------------------------------------------------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉霍拉卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值