java发送HTTP GET POST请求

java发送HTTP GET POST请求
1、发送源码
import  java.io.BufferedReader;
import  java.io.BufferedWriter;
import  java.io.File;
import  java.io.FileReader;
import  java.io.IOException;
import  java.io.OutputStream;
import  java.io.OutputStreamWriter;
import  java.net.HttpURLConnection;
import  java.net.URL;



/**
 * 通过HTTP上传文件
 * 
@author  [email protected]
 * @create 2008-4-14
 
*/

public   class  PostUrl {

    HttpURLConnection conn 
=   null ;
    BufferedWriter bWriter 
=   null ;
    OutputStream os 
=   null ;
    OutputStreamWriter osw 
=   null ;
    FileReader fr 
=   null ;
    BufferedReader br 
=   null ;
    String str 
=   "" ;
    
char  c  =   ' \n ' ;
    String filename 
=   " d:/shchinamobile.log " ;

    

    
/**
     * 通过HTTP协议以POST形式发送指定文件至指定url
     * 
@param  url
     * 
@throws  IOException
     
*/
    
public   void  sendPost(String url) {
        
        
try  {
            File f 
=   new  File(filename);
            
            URL url1 
=   new  URL(url);
            conn 
=  (HttpURLConnection)url1.openConnection();
            conn.setRequestMethod(
" POST " );
            conn.setRequestProperty(
" Content-Type " " application/x-www-form-urlencoded " );
            conn.setRequestProperty(
" Connection " " Keep-Alive " );
            conn.setUseCaches( 
false );
            conn.setDoOutput(
true );
            osw 
=   new  OutputStreamWriter(conn.getOutputStream());
            fr 
=   new  FileReader(f);
            br 
=   new  BufferedReader(fr);
            
while ((str  =  br.readLine())  !=   null ) {
                osw.write(str);
                osw.write(c);
            }
            osw.flush() ;
            osw.close() ;
            
            
if  (conn.getResponseCode()  !=  HttpURLConnection.HTTP_OK)
                System.out.println( 
" connect failed! " );
        } 
catch  (IOException e) {
            e.printStackTrace(System.out);
        }
        
finally
        {
            
if  (osw  !=   null )
                
try  {
                    osw.close() ;
                } 
catch  (IOException e1) {
                    e1.printStackTrace(System.out);
                }
            
            
if  (conn  !=   null )
                conn.disconnect() ;
        }
    }    

    
public   static   void  main(String[] args) {
        PostUrl pu 
=   new  PostUrl();
        
try  {
            pu.sendPost(
" http://127.0.0.1:8080/testjava/testp.jsp " );
        } 
catch  (Exception e) {
            e.printStackTrace();
        }
    }
}

2、接收JSP
<%
     System.err.println(
" This a struts page. " );
     
         
// 接收上传的文件内容的临时文件的文件名
        String tempFileName = new  String( " tempFileName1 " );
        
// tempFile1对象指向这个临时文件
        File tempFile1 = new  File( " D:/ " ,tempFileName);
        
// outputFile1文件输出流指向这个临时文件
        FileOutputStream outputFile1 = new  FileOutputStream(tempFile1);
        
// 得到客户端提交的所有数据
        InputStream fileSource1 = request.getInputStream();
        
// 将得到的客户端的数据写入临时文件
         byte  b[] = new   byte [ 1000 ];
        
int  n;
        
while ((n = fileSource1.read(b)) !=- 1 )
            outputFile1.write(b,
0 ,n);
        
// 关闭输出流和输入流
        outputFile1.close();
        fileSource1.close();
      
     
%>

已测试通过

还可以通HttpClient来发送,请参照: http://www.blogjava.net/willpower88/archive/2008/04/14/192679.html

你可能感兴趣的:(java发送HTTP GET POST请求)