欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡(jiǎn)單介紹Java網(wǎng)絡(luò)編程中的HTTP請(qǐng)求

 更新時(shí)間:2015年09月24日 09:07:57   投稿:goldensun  
這篇文章主要介紹了簡(jiǎn)單介紹Java網(wǎng)絡(luò)編程中的HTTP請(qǐng)求,需要的朋友可以參考下

HTTP請(qǐng)求的細(xì)節(jié)——請(qǐng)求行
 
  請(qǐng)求行中的GET稱之為請(qǐng)求方式,請(qǐng)求方式有:POST、GET、HEAD、OPTIONS、DELETE、TRACE、PUT,常用的有: GET、 POST
  用戶如果沒有設(shè)置,默認(rèn)情況下瀏覽器向服務(wù)器發(fā)送的都是get請(qǐng)求,例如在瀏覽器直接輸?shù)刂吩L問(wèn),點(diǎn)超鏈接訪問(wèn)等都是get,用戶如想把請(qǐng)求方式改為post,可通過(guò)更改表單的提交方式實(shí)現(xiàn)。
  不管POST或GET,都用于向服務(wù)器請(qǐng)求某個(gè)WEB資源,這兩種方式的區(qū)別主要表現(xiàn)在數(shù)據(jù)傳遞上:如果請(qǐng)求方式為GET方式,則可以在請(qǐng)求的URL地址后以?的形式帶上交給服務(wù)器的數(shù)據(jù),多個(gè)數(shù)據(jù)之間以&進(jìn)行分隔,例如:GET /mail/1.html?name=abc&password=xyz HTTP/1.1
  GET方式的特點(diǎn):在URL地址后附帶的參數(shù)是有限制的,其數(shù)據(jù)容量通常不能超過(guò)1K。
  如果請(qǐng)求方式為POST方式,則可以在請(qǐng)求的實(shí)體內(nèi)容中向服務(wù)器發(fā)送數(shù)據(jù),Post方式的特點(diǎn):傳送的數(shù)據(jù)量無(wú)限制。
 
HTTP請(qǐng)求的細(xì)節(jié)——消息頭

 
  HTTP請(qǐng)求中的常用消息頭
 
  accept:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,它所支持的數(shù)據(jù)類型
  Accept-Charset: 瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,它支持哪種字符集
  Accept-Encoding:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,支持的壓縮格式
  Accept-Language:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,它的語(yǔ)言環(huán)境
  Host:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,想訪問(wèn)哪臺(tái)主機(jī)
  If-Modified-Since: 瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,緩存數(shù)據(jù)的時(shí)間
  Referer:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,客戶機(jī)是哪個(gè)頁(yè)面來(lái)的  防盜鏈
  Connection:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,請(qǐng)求完后是斷開鏈接還是何持鏈接

例:

http_get

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
 
 
public class Http_Get { 
 
  private static String URL_PATH = "http://192.168.1.125:8080/myhttp/pro1.png"; 
 
  public Http_Get() { 
    // TODO Auto-generated constructor stub 
  } 
 
  public static void saveImageToDisk() { 
    InputStream inputStream = getInputStream(); 
    byte[] data = new byte[1024]; 
    int len = 0; 
    FileOutputStream fileOutputStream = null; 
    try { 
      fileOutputStream = new FileOutputStream("C:\\test.png"); 
      while ((len = inputStream.read(data)) != -1) { 
        fileOutputStream.write(data, 0, len); 
      } 
    } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } finally { 
      if (inputStream != null) { 
        try { 
          inputStream.close(); 
        } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
        } 
      } 
      if (fileOutputStream != null) { 
        try { 
          fileOutputStream.close(); 
        } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
        } 
      } 
    } 
  } 
 
  /** 
   * 獲得服務(wù)器端的數(shù)據(jù),以InputStream形式返回 
   * @return 
   */ 
  public static InputStream getInputStream() { 
    InputStream inputStream = null; 
    HttpURLConnection httpURLConnection = null; 
    try { 
      URL url = new URL(URL_PATH); 
      if (url != null) { 
        httpURLConnection = (HttpURLConnection) url.openConnection(); 
        // 設(shè)置連接網(wǎng)絡(luò)的超時(shí)時(shí)間 
        httpURLConnection.setConnectTimeout(3000); 
        httpURLConnection.setDoInput(true); 
        // 表示設(shè)置本次http請(qǐng)求使用GET方式請(qǐng)求 
        httpURLConnection.setRequestMethod("GET"); 
        int responseCode = httpURLConnection.getResponseCode(); 
        if (responseCode == 200) { 
          // 從服務(wù)器獲得一個(gè)輸入流 
          inputStream = httpURLConnection.getInputStream(); 
        } 
      } 
    } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
    return inputStream; 
  } 
 
  public static void main(String[] args) { 
    // 從服務(wù)器獲得圖片保存到本地 
    saveImageToDisk(); 
  } 
} 

Http_Post

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.HashMap; 
import java.util.Map; 
 
public class Http_Post { 
 
  // 請(qǐng)求服務(wù)器端的url 
  private static String PATH = "http://192.168.1.125:8080/myhttp/servlet/LoginAction"; 
  private static URL url; 
 
  public Http_Post() { 
    // TODO Auto-generated constructor stub 
  } 
 
  static { 
    try { 
      url = new URL(PATH); 
    } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
  } 
 
  /** 
   * @param params 
   *      填寫的url的參數(shù) 
   * @param encode 
   *      字節(jié)編碼 
   * @return 
   */ 
  public static String sendPostMessage(Map<String, String> params, 
      String encode) { 
    // 作為StringBuffer初始化的字符串 
    StringBuffer buffer = new StringBuffer(); 
    try { 
      if (params != null && !params.isEmpty()) { 
         for (Map.Entry<String, String> entry : params.entrySet()) { 
            // 完成轉(zhuǎn)碼操作 
            buffer.append(entry.getKey()).append("=").append( 
                URLEncoder.encode(entry.getValue(), encode)) 
                .append("&"); 
          } 
        buffer.deleteCharAt(buffer.length() - 1); 
      } 
      // System.out.println(buffer.toString()); 
      // 刪除掉最有一個(gè)& 
       
      System.out.println("-->>"+buffer.toString()); 
      HttpURLConnection urlConnection = (HttpURLConnection) url 
          .openConnection(); 
      urlConnection.setConnectTimeout(3000); 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setDoInput(true);// 表示從服務(wù)器獲取數(shù)據(jù) 
      urlConnection.setDoOutput(true);// 表示向服務(wù)器寫數(shù)據(jù) 
      // 獲得上傳信息的字節(jié)大小以及長(zhǎng)度 
      byte[] mydata = buffer.toString().getBytes(); 
      // 表示設(shè)置請(qǐng)求體的類型是文本類型 
      urlConnection.setRequestProperty("Content-Type", 
          "application/x-www-form-urlencoded"); 
      urlConnection.setRequestProperty("Content-Length", 
          String.valueOf(mydata.length)); 
      // 獲得輸出流,向服務(wù)器輸出數(shù)據(jù) 
      OutputStream outputStream = urlConnection.getOutputStream(); 
      outputStream.write(mydata,0,mydata.length); 
      outputStream.close(); 
      // 獲得服務(wù)器響應(yīng)的結(jié)果和狀態(tài)碼 
      int responseCode = urlConnection.getResponseCode(); 
      if (responseCode == 200) { 
        return changeInputStream(urlConnection.getInputStream(), encode); 
      } 
    } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
    return ""; 
  } 
 
  /** 
   * 將一個(gè)輸入流轉(zhuǎn)換成指定編碼的字符串 
   * 
   * @param inputStream 
   * @param encode 
   * @return 
   */ 
  private static String changeInputStream(InputStream inputStream, 
      String encode) { 
    // TODO Auto-generated method stub 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    byte[] data = new byte[1024]; 
    int len = 0; 
    String result = ""; 
    if (inputStream != null) { 
      try { 
        while ((len = inputStream.read(data)) != -1) { 
          outputStream.write(data, 0, len); 
        } 
        result = new String(outputStream.toByteArray(), encode); 
      } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
    } 
    return result; 
  } 
 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Map<String, String> params = new HashMap<String, String>(); 
    params.put("username", "admin"); 
    params.put("password", "123"); 
    String result = Http_Post.sendPostMessage(params, "utf-8"); 
    System.out.println("--result->>" + result); 
  } 
 
} 


相關(guān)文章

最新評(píng)論