小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Java 模擬并發(fā)訪問

 昵稱10504424 2013-02-19
  1. import java.io.DataInputStream;         
  2. import java.io.DataOutputStream;             
  3. import java.net.HttpURLConnection;         
  4. import java.net.URL;             
  5. import java.util.Map;    
  6. import java.util.Properties;   
  7.   
  8. public class NetUtils   
  9. {   
  10.        
  11.     public static final String CHARACTER_ENCODING = "UTF-8";         
  12.   public static final String PATH_SIGN = "/";         
  13.   public static final String METHOD_POST = "POST";         
  14.   public static final String METHOD_GET = "GET";         
  15.   public static final String CONTENT_TYPE = "Content-Type";         
  16.       
  17.   /**      
  18.    * 以POST方式向指定地址發(fā)送數(shù)據(jù)包請求,并取得返回的數(shù)據(jù)包      
  19.    *       
  20.    * @param urlString      
  21.    * @param requestData      
  22.    * @return 返回?cái)?shù)據(jù)包      
  23.    * @throws Exception      
  24.    */        
  25.   public static byte[] requestPost(String urlString, byte[] requestData)         
  26.           throws Exception   
  27.   {         
  28.       Properties requestProperties = new Properties();         
  29.       requestProperties.setProperty(CONTENT_TYPE,         
  30.               "application/octet-stream; charset=utf-8");         
  31.       
  32.       return requestPost(urlString, requestData, requestProperties);         
  33.   }         
  34.   
  35.   /**      
  36.    * 以POST方式向指定地址發(fā)送數(shù)據(jù)包請求,并取得返回的數(shù)據(jù)包      
  37.    *       
  38.    * @param urlString      
  39.    * @param requestData      
  40.    * @param requestProperties      
  41.    * @return 返回?cái)?shù)據(jù)包      
  42.    * @throws Exception      
  43.    */        
  44.   public static byte[] requestPost(String urlString, byte[] requestData,         
  45.           Properties requestProperties) throws Exception   
  46.   {   
  47.       byte[] responseData = null;         
  48.       HttpURLConnection con = null;         
  49.       
  50.       try {         
  51.           URL url = new URL(urlString);         
  52.           con = (HttpURLConnection) url.openConnection();         
  53.           //設(shè)置請求屬性   
  54.           if ((requestProperties != null) && (requestProperties.size() > 0))   
  55.           {         
  56.               for (Map.Entry<Object, Object> entry : requestProperties.entrySet())   
  57.               {         
  58.                   String key = String.valueOf(entry.getKey());         
  59.                   String value = String.valueOf(entry.getValue());         
  60.                   con.setRequestProperty(key, value);         
  61.               }         
  62.           }         
  63.       
  64.           con.setRequestMethod(METHOD_POST); // 置為POST方法         
  65.       
  66.           con.setDoInput(true); // 開啟輸入流         
  67.           con.setDoOutput(true); // 開啟輸出流         
  68.       
  69.           // 如果請求數(shù)據(jù)不為空,輸出該數(shù)據(jù)。         
  70.           if (requestData != null)   
  71.           {         
  72.               DataOutputStream dos = new DataOutputStream(con.getOutputStream());         
  73.               dos.write(requestData);   
  74.               dos.flush();         
  75.               dos.close();         
  76.           }         
  77.       
  78.           int length = con.getContentLength();         
  79.           // 如果回復(fù)消息長度不為-1,讀取該消息。         
  80.           if (length != -1)   
  81.           {         
  82.               DataInputStream dis = new DataInputStream(con.getInputStream());         
  83.               responseData = new byte[length];         
  84.               dis.readFully(responseData);         
  85.               dis.close();         
  86.           }         
  87.       }   
  88.       catch (Exception e)   
  89.       {         
  90.           throw e;         
  91.       }   
  92.       finally  
  93.       {         
  94.           if (con != null)   
  95.           {         
  96.               con.disconnect();         
  97.               con = null;         
  98.           }         
  99.       }         
  100.       return responseData;         
  101.   }         
  102.   
  103. }  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多