博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java发送https请求
阅读量:4219 次
发布时间:2019-05-26

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

import java.net.*; import java.io.*; import java.security.*; import javax.net.ssl.*; import com.macfaq.io.*; public class HTTPSClient {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java HTTPSClient2 host"); return; } int port = 443; // default https port String host = args[0]; try {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket socket = (SSLSocket) factory.createSocket(host, port); // enable all the suites String[] supported = socket.getSupportedCipherSuites(); socket.setEnabledCipherSuites(supported); Writer out = new OutputStreamWriter(socket.getOutputStream()); // https requires the full URL in the GET line out.write("GET http://" + host + "/ HTTP/1.1\r\n"); out.write("Host: " + host + "\r\n"); out.write("\r\n"); out.flush(); // read response BufferedReader in = new SafeBufferedReader( new InputStreamReader(socket.getInputStream())); // read the header String s; while (!(s = in.readLine()).equals("")) {
System.out.println(s); } System.out.println(); // read the length String contentLength = in.readLine(); int length = Integer.MAX_VALUE; try {
length = Integer.parseInt(contentLength.trim(), 16); } catch (NumberFormatException ex) {
// This server doesn't send the content-length // in the first line of the response body } System.out.println(contentLength); int c; int i = 0; while ((c = in.read()) != -1 && i++ < length) {
System.out.write(c); } System.out.println(); out.close(); in.close(); socket.close(); } catch (IOException ex) {
System.err.println(ex); } } } /** * Java Network Programming, Third Edition * By Elliotte Rusty Harold * Third Edition October 2004 * ISBN: 0-596-00721-3 */

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

你可能感兴趣的文章
2018诺贝尔经济学奖得主,一名62岁的Python教徒
查看>>
从代码恐惧到开发大牛:开发者“10倍提升”宝典
查看>>
吴恩达机器学习课程:完全用Python完成,可以的!(附代码)
查看>>
狂破11项记录,谷歌年度最强NLP论文到底强在哪里?
查看>>
AI“重造”麻省理工学院!今宣布投资10亿美元成立全新计算学院,近70年来最大结构调整...
查看>>
波音公司探索飞行AI,模拟人类大脑制造芯片!
查看>>
清华大学与MIT合作推出数据科学与信息技术「微硕士」学位
查看>>
信阳新闻网:《观察+思考+努力=创新——访南京大数据研究院院长刘鹏》
查看>>
广东电信20多位经理到访云创参观交流
查看>>
秦淮区科协主席付煜一行莅临云创调研指导
查看>>
广东省民营企业合作交流协会会长谭铭卓一行到访
查看>>
云创大数据亮相2017世界物联网博览会,六因子环境监测仪等引关注热潮
查看>>
云创冬日紫金山踏雪游记
查看>>
西安思源学院电子信息工程学院院长张卫钢一行到访
查看>>
邀请函|欢迎参加2019云创大数据实验平台金融类/电子商务类/数学统计类院校各省总代理招募大会!...
查看>>
云创大数据的2018年!
查看>>
【回顾】云创大数据地震事业部成立
查看>>
全国高校(高职)大数据师资培训班圆满落幕,200多名老师抢先“尝鲜”!
查看>>
【回顾】云创大数据教育事业部成立
查看>>
云创大数据与江苏城市职业学院共建实习实训就业基地!
查看>>