博客
关于我
java:后台调用接口链接方式(HttpURLConnection)
阅读量:159 次
发布时间:2019-02-28

本文共 1262 字,大约阅读时间需要 4 分钟。

Java HTTP ??????

?????

??????? HTTP ??????????????????

1. ?? URL ?????

URL url = new URL(path);HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

2. ???????????

httpURLConnection.setRequestMethod(method);httpURLConnection.setRequestProperty("accept", "*/*");httpURLConnection.setRequestProperty("connection", "Keep-Alive");httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

3. ?????????

OutputStreamWriter out = new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8");out.write(data);out.flush();InputStream inputStream = httpURLConnection.getInputStream();br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));String str = "";while ((str = br.readLine()) != null) {    result.append(str);}return result.toString();

????

1. URL ???????

????? new URL(path) ?? URL ?????????? HttpURLConnection ??????????????????? HTTP ?????????????

2. ??????

???????????????????????????? method?????????????????????????????????????????????

???setDoOutput(true) ? setDoInput(true) ????????????????????????????????

3. ?????????

?? OutputStreamWriter ???? UTF-8 ????????????????????????? InputStreamReader ?????????????????????????????

????

?????????? JSON ????????????????????????????????

转载地址:http://eipc.baihongyu.com/

你可能感兴趣的文章
PHP OAuth 2.0 Server
查看>>
php odbc驱动,php常用ODBC函数集(详细)
查看>>
php openssl aes ecb,php openssl_encrypt AES-128-ECB iOS
查看>>
php paypal rest api,PayPal REST API指定网络配置文件PHP
查看>>
php pcntl 多进程学习
查看>>
PHP pcntl_fork不能在web服务器中使用的变通方法
查看>>
php private ,public protected三者的区别
查看>>
php PSR规范
查看>>
php rand() 重复,array_rand()函数从另外一个数组中随机取得的一定数量的数组的元素是否会重复?...
查看>>
php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用
查看>>
php redis 集群扩展类文件
查看>>
php redis(2)
查看>>
PHP Redis分布式锁
查看>>
php redis的应用
查看>>
php session超时时间_php怎么设置session超时时间
查看>>
PHP SOAP模块的使用方法:NON-WSDL模式
查看>>
PHP Socket实现websocket(三)Stream函数
查看>>
php Socket通信
查看>>
PHP SPL标准库-迭代器
查看>>
php static 变量
查看>>