分享

java socket实现http

 hh3755 2011-08-30

import java.net.Socket;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.File;


public class Httpclient {
public String boundary = "-----=_Part_4_20727434.1184328927468";
private static final byte CR = (byte)'\r';
private static final byte LF = (byte)'\n';
private static final byte[] CRLF = new byte[]{CR,LF};
private String contentType = "multipart/form-data; type=\"text/xml\"; start=\"<A1DD1D17CA63B74B4021F4C2EE55D783>\"; .boundary="+boundary;
private Socket socket;
private String host; 
private int port;
private String uri;
private byte[] key; //AES128CBC的密钥
private byte[] iv;   //AES128CBC的初始向量

public void setUri(String uri)
{
   this.uri=uri;
}
public void setKey(byte[] key)
{this.key =key;}

public void setIv(byte[] iv)
{this.iv =iv;}

public Httpclient(String host,int port){
        this.host = host;
        this.port = port;
}

private void openServer() throws Exception {
        socket = new Socket(host,port);
}

private void closeServer() {
   try{
        if(socket!=null){ socket.close();}
        System.out.println("END");
   }
        catch(Exception e){e.printStackTrace();}
   }


private void write(OutputStream out,String msg) throws IOException 
    { out.write(msg.getBytes());
    }


private void addHead(long contentLength,OutputStream out) throws   IOException {        
        write(out,"POST "+uri+" HTTP/1.1");
        out.write(CRLF);
        write(out,"Content-Type: "+contentType);
        out.write(CRLF);
        write(out,"Accept: application/soap+xml, application/dime, multipart/related, text/*"); 
        out.write(CRLF);
        write(out,"User-Agent: Axis/1.2.1");
        out.write(CRLF);
        write(out,"Host:"+host+":"+Integer.toString(port));
        out.write(CRLF);
        write(out,"Cache-Control: no-cache");
        out.write(CRLF);
        write(out,"Pragma: no-cache");
        out.write(CRLF);
        write(out,"SOAPAction: \"\"");
        out.write(CRLF);
        write(out,"Content-Length: "+contentLength);
        out.write(CRLF);
        out.write(CRLF);    
       
}

public void addgethead(OutputStream out) throws   IOException
{
   write(out,"GET "+uri+" HTTP/1.1");
        out.write(CRLF);
        write(out,"Accept: */*");
        out.write(CRLF);
        write(out,"Accept-Language: zh-cn"); 
        out.write(CRLF);
        write(out,"UA-CPU: x86");
        out.write(CRLF);
        write(out,"Accept-Encoding: gzip, deflate");
        out.write(CRLF);
        write(out,"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)");
        out.write(CRLF);
        write(out,"Host:"+host+":"+Integer.toString(port));
        out.write(CRLF); 
        write(out,"Connection: Keep-Alive");
        out.write(CRLF);
        out.write(CRLF);   
}

private void addXML(File file,OutputStream out) throws   IOException {
   write(out,boundary);
        out.write(CRLF);       
        write(out,"Content-Type: "+getType(file)); out.write(CRLF);
        write(out,"Content-Transfer-Encoding: binary");out.write(CRLF);
        write(out,"Content-Id: <A1DD1D17CA63B74B4021F4C2EE55D783>");
        out.write(CRLF);
        out.write(CRLF);
        InputStream is = new FileInputStream(file);
        byte[] b = new byte[1024];
        int count = is.read(b);
        while(count!=-1){
        out.write(b,0,count);
            count = is.read(b);
        }
        is.close();
        out.write(CRLF);
        } 
private void addBody(File file,OutputStream out) throws IOException {
   write(out,boundary);
        out.write(CRLF);
       
        write(out,"Content-Type: "+getType(file)); out.write(CRLF);
        write(out,"Content-Transfer-Encoding: binary");out.write(CRLF);
        write(out,"Content-Id: <A1DD1D17CA63B74B4021F4C2EE55D783>");
        out.write(CRLF);
        out.write(CRLF);
        InputStream is = new FileInputStream(file);
        ASE128CBC f1=new ASE128CBC();
        try{
        f1.init(0, key, iv) ;   
        byte[] cipher=new byte[1024]; //生成的密文
        byte[] b = new byte[1024];      //读取的明文 
        int count = is.read(b);
        f1.update(b, 0, count, cipher, 0);//加密
        while(count!=-1){
        out.write(cipher,0,count);
        if(count<1024)break;
            count = is.read(b);
            f1.update(b, 0, count, cipher, 0);
        }
        is.close();
        out.write(CRLF);
        }catch(Exception e){e.printStackTrace();}
       }

public void uploadFile(File[] files) throws Exception {
   openServer();
   OutputStream out = socket.getOutputStream();
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
   addXML(files[0],bos);
   for(int i=1;i<files.length;i++){
             if(files[i]==null)break;
    addBody(files[i],bos);
   }
   write(bos,boundary+"--");
   bos.write(CRLF);
   addHead(bos.size(),bos1);
   // addgethead(bos1);
   bos.writeTo(bos1);
   bos1.writeTo(out);
   bos.close();
   bos1.close(); 
   out.flush();
   closeServer();
   }
public String getType(File file){
   String tem=file.toString();
   if(tem.endsWith(".jpg"))tem="img/jpeg";
   else if(tem.endsWith(".xml"))tem="text/xml";
   else tem="Unknown";
    return tem;
  
}

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多