简易的Java版HttpClient

  1. /**
  2.  * This is a test program for HttpClient.  @author Hu Dennis created at 2008-11-12
  3.  */
  4. import java.net.Socket;
  5. import java.io.BufferedReader;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. public class HttpClient {
  11.     public static void main(String[] args) {
  12.         String dstHost = "10.56.135.64";
  13.         int    dstPort = 80;
  14.         
  15.         Socket clientSocket = null;
  16.         
  17.         DataOutputStream outBound = null;
  18.         DataInputStream  inBound  = null;
  19.         
  20.         try {
  21.             
  22.             clientSocket = new Socket(dstHost, dstPort);
  23.             clientSocket.setSoTimeout(30000);           
  24.             
  25.             outBound = new DataOutputStream(clientSocket.getOutputStream());
  26.             inBound  = new DataInputStream(clientSocket.getInputStream());
  27.             
  28.             StringBuilder request = new StringBuilder(); 
  29.             
  30.             request.append("GET /test.html HTTP/1.1/n");
  31.             request.append("HOST:10.56.135.64/n/r/n");
  32.             
  33.             outBound.writeBytes(request.toString());
  34.             
  35.             
  36.             BufferedReader bfReader = new BufferedReader(new InputStreamReader(inBound)); 
  37.             
  38.             String responseLine = null;
  39.             
  40.             while ((responseLine = bfReader.readLine()) != null){   
  41.                 
  42.                 System.out.println(responseLine);   
  43.                 if ( responseLine.indexOf("</HTML>") != -1 || responseLine.indexOf("</html>") != -1)   
  44.                     break;   
  45.             }   
  46.             
  47.             outBound.close();
  48.             inBound.close();
  49.             clientSocket.close();
  50.             
  51.         }
  52.         catch (IOException e) {
  53.             System.out.println(e);
  54.         }
  55.         
  56.         System.out.println("This is the end of the program.");
  57.     }
  58. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gobitan

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

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

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

打赏作者

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

抵扣说明:

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

余额充值