现在的位置: 首页 > 综合 > 正文

urlConnection

2019年03月05日 ⁄ 综合 ⁄ 共 3367字 ⁄ 字号 评论关闭
原文地址:urlConnection作者:起伏
package cn.core.util;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public   class  HttpInvoker
  {
 
    
 public   static
  final  String GET_URL
 =   "http://www.baidu.cn";
    
 public   static
  final  String POST_URL
 =   "http://www.baidu.cn";
    
 public   static
  void  readContentFromGet()
 throws  IOException{
    
     //
 拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码 
    
    //String getURL
 =  GET_URL  +
  "?username=" +
 URLEncoder.encode("fatman" ,"utf-8");
     String
getURL  =  GET_URL;
    
    URL getUrl
 =   new
 URL(getURL);
    
     //
 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
    
     //
 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection 
    
    HttpURLConnection connection
 =  (HttpURLConnection)
getUrl.openConnection();
    
     //
 进行连接,但是实际上get
request要在下一句的connection.getInputStream()函数中才会真正发到
    
     //
 服务器 
    
    connection.connect();
    
     //
 取得输入流,并使用Reader读取 
    
    BufferedReader reader
 =   new
 BufferedReader( new
 InputStreamReader(connection.getInputStream()));
    
    System.out.println( "
============================= " );
    
    System.out.println( "
Contents of get request " );
    
    System.out.println( "
============================= " );
    
    String lines;
    
    while((lines
 =  reader.readLine())
 !=   null ){
    
     
  System.out.println(lines);
    
   
    
    reader.close();
    
   
connection.disconnect();
    
    System.out.println( "
============================= " );
    
    System.out.println( "
Contents of get request ends " );
    
    System.out.println( "
============================= " );
    
 
     public
  static   void
 readContentFromPost()  throws
 IOException{
    
     //
 Post请求的url,与get不同的是不需要带参数 
    
    URL postUrl
 =   new
 URL(POST_URL);
    
     //
 打开连接 
    
    HttpURLConnection connection
 =  (HttpURLConnection)
postUrl
    
     
     
.openConnection();
    
     //
 设置是否向connection输出,因为这个是post请求,参数要放在
    
     //
 http正文内,因此需要设为true 
    
    connection.setDoOutput( true
);
    
    connection.setDoInput( true
);
    
    //有的请求不支持POST请求
    
   
//connection.setRequestMethod( "POST" );
    
    connection.setUseCaches(
false );
    
   
connection.setInstanceFollowRedirects( true );
    
   
//connection.setRequestProperty("Content-Type"
,"application/x-www-form-urlencoded");
    
   
connection.setRequestProperty("Content-Type" ,"text/html");
    
    connection.connect();
//      
  DataOutputStream out  =
  new
 DataOutputStream(connection.getOutputStream());
//      
  String content ="firstname=" +
 URLEncoder.encode( "一个大肥人" ,
 "utf-8" );
//      
  out.writeBytes(content); 
//      
  out.flush();
//      
  out.close();  //
 flush and close 
    
    BufferedReader reader
 =   new
 BufferedReader( new
 InputStreamReader(connection.getInputStream()));
    
    String line;
    
    System.out.println( "
============================= " );
    
    System.out.println( "
Contents of post request " );
    
    System.out.println( "
============================= " );
    
      while
 ((line  =
 reader.readLine())  !=
  null ){
    
     
  System.out.println(line);
    
   
    
    System.out.println( "
============================= " );
    
    System.out.println( "
Contents of post request ends " );
    
    System.out.println( "
============================= " );
    
    reader.close();
    
   
connection.disconnect();
    
 
    
   
    
  public   static
  void  main(String[]
args){
    
     //
 TODO Auto-generated method
stub 
    
      try{
    
     
  //readContentFromGet();
    
     
  readContentFromPost();
    
     }
  catch  (IOException e){
    
     
   //  TODO
Auto-generated catch block 
    
     
  e.printStackTrace();
    
   
    
 
 

抱歉!评论已关闭.