• 大小: 12KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: Java
  • 标签: http  客户端  

资源简介

http客户端java程序,代码质量高,经过测试。

资源截图

代码片段和文件信息

package com.tcl.dhome.server.protocol.http;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Properties;

public class HttpClient{
    protected Socket socket;
    protected BufferedOutputStream sender; 
    protected BufferedInputStream receiver;
    protected ByteArrayInputStream byteStream;   
  
    protected URL targetUrl;   
  
    private int responseCode = -1; 
    private String responseMessage = ““;
    private String serverVersion = ““;   
 
    private int responseLength =0;
    
    private Properties headerProp = new Properties();
  
    public HttpClient(){}
  
    public HttpClient(String url){
        GET(url);
    }   
  
  
    /* GET方法根据URL,会请求文件、数据库查询结果、程序运行结果等多种内容 */  
  
    public void GET(String url){   
      try{ 
            checkHTTP(url);
            openServer(targetUrl.getHost()targetUrl.getPort());
            String cmd = “GET “ + getURLFormat(targetUrl) + “ HTTP/1.0\r\n“ 
                                             + getbaseHeads() + “\r\n“;
            sendMessage(cmd);  
            receiveMessage(); 
        }
        catch(ProtocolException p){
            p.printStackTrace();
            return;
        }
        catch(UnknownHostException e){
            e.printStackTrace();
            return; 
        } 
        catch(IOException i){
            i.printStackTrace();
            return;
        } 
    }   
  
  
    /* 
     * HEAD方法只请求URL的元信息,不包括URL本身。若怀疑本机和服务器上的 
     * 文件相同,用这个方法检查最快捷有效。 
     */
    public void HEAD(String url){
        try{ 
            checkHTTP(url);
            openServer(targetUrl.getHost()targetUrl.getPort());
            String cmd = “HEAD “ + getURLFormat(targetUrl) + “ HTTP/1.0\r\n“  
                                             + getbaseHeads() + “\r\n“; 
            sendMessage(cmd);
            receiveMessage();
        }  
        catch(ProtocolException p){
            p.printStackTrace();
            return; 
        }
        catch(UnknownHostException e){
            e.printStackTrace();
            return;
        }
        catch(IOException i){ 
            i.printStackTrace(); 
            return; 
        } 
    } 
    
    /* 
     * POST方法是向服务器传送数据,以便服务器做出相应的处理。例如网页上常用的 
     * 提交表格。
     */ 
    public void POST(String url String content){ 
        try{
            checkHTTP(url);
            openServer(targetUrl.getHost()targetUrl.getPort()); 
            String cmd = “POST “ + getURLFormat(targetUrl) + “ HTTP/1.0\r\n“  
                                         + getbaseHea

评论

共有 条评论