zoukankan      html  css  js  c++  java
  • HTTP Client 编写

    手头生正好有一个HTTP Client例子,使用socket 连接。代码如下:

    运行上述代码,返回如下:

    IP Address is : 211.100.26.77
    HTTP/1.1 302 Moved Temporarily
    Server: nginx/0.7.65
    Date: Fri, 02 Jul 2010 14:27:19 GMT
    Content-Type: text/html
    Content-Length: 161
    Connection: keep-alive
    Location: http://www.csdn.net/

    <html>
    <head><title>302 Found</title></head>
    <body bgcolor="white">
    <center><h1>302 Found</h1></center>
    <hr><center>nginx/0.7.65</center>
    </body>
    </html>

    这个代码基本上是一个最原始的http clinet,模拟了http协议,比如协议头。但是这段代码有几个问题:

    首先不能解析动态网页,如aspx等(不知道为什么,请知道的朋友告知,谢谢!)。

    其次,服务器返回的是一个二进制byte[],你需要自己解析得到的你需要的内容。

    因为我需要拿到的是一个二进制的png图片文件,并不关心头信息,如果能有一个library直接提供解析功能就好了。

    Apache common HttpClient 库提供了很好的封装,因此非常简单的几行代码(不及异常处理)就可以了。

    但是这个代码有时会给出如下警告:

    Jul 2, 2010 11:42:34 AM org.apache.commons.httpclient.HttpMethodBase getResponseBody
    WARNING: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

    按照提示应该使用getResponseBodyAsStream。其中最后还有一个将byte[]生成图片并显示,并且判断图片大小后,重新设置图片大小的。下面是我的完整实例:

    参考:

    http://hc.apache.org/httpclient-3.x/tutorial.html

    http://hc.apache.org/httpclient-3.x/apidocs/index.html

  • 相关阅读:
    Atitit s2018.6 s6 doc list on com pc.docx Atitit s2018.6 s6 doc list on com pc.docx  Aitit algo fix 算法系列补充.docx Atiitt 兼容性提示的艺术 attilax总结.docx Atitit 应用程序容器化总结 v2 s66.docx Atitit file cms api
    Atitit s2018.5 s5 doc list on com pc.docx  v2
    Atitit s2018.5 s5 doc list on com pc.docx  Acc 112237553.docx Acc baidu netdisk.docx Acc csdn 18821766710 attilax main num.docx Atiitt put post 工具 开发工具dev tool test.docx Atiitt 腾讯图像分类相册管家.docx
    Atitit s2018 s4 doc list dvchomepc dvccompc.docx .docx s2018 s4 doc compc dtS44 s2018 s4 doc dvcCompc dtS420 s2018 s4f doc homepc s2018 s4 doc compc dtS44(5 封私信 _ 44 条消息)WebSocket 有没有可能取代 AJAX
    Atitit s2018 s3 doc list alldvc.docx .docx s2018 s3f doc compc s2018 s3f doc homepc sum doc dvcCompc dtS312 s2018 s3f doc compcAtitit PathUtil 工具新特性新版本 v8 s312.docx s2018 s3f doc compcAtitit 操作日
    Atitit s2018.2 s2 doc list on home ntpc.docx  Atiitt uke制度体系 法律 法规 规章 条例 国王诏书.docx Atiitt 手写文字识别 讯飞科大 语音云.docx Atitit 代码托管与虚拟主机.docx Atitit 企业文化 每日心灵 鸡汤 值班 发布.docx Atitit 几大研发体系对比 Stage-Gat
    Atitit 文员招募规范 attilax总结
    Atitit r2017 r6 doc list on home ntpc.docx
    atitit r9 doc on home ntpc .docx
    Atitit.如何文章写好 论文 文章 如何写好论文 技术博客 v4
  • 原文地址:https://www.cnblogs.com/ainima/p/6331333.html
Copyright © 2011-2022 走看看