java http請求

public static void getUploadInformation(String  path,String obj) throws IOException, JSONException {
        //建立鏈接
        URL url = new URL(path);
        HttpURLConnection connection ;
        StringBuffer sbuffer=null;
        try {
        //添加 請求內容
            connection= (HttpURLConnection) url.openConnection();
            //設置http鏈接屬性
            connection.setDoOutput(true);// http正文內,所以須要設爲true, 默認狀況下是false;
            connection.setDoInput(true);// 設置是否從httpUrlConnection讀入,默認狀況下是true;
            connection.setRequestMethod("PUT"); // 能夠根據須要 提交 GET、POST、DELETE、PUT等http提供的功能
            //connection.setUseCaches(false);//設置緩存,注意設置請求方法爲post不能用緩存
            // connection.setInstanceFollowRedirects(true);

            connection.setRequestProperty("Host", "*******");  //設置請 求的服務器網址,域名,例如***.**.***.***
            connection.setRequestProperty("Content-Type", " application/json");//設定 請求格式 json,也能夠設定xml格式的
            connection.setRequestProperty("Accept-Charset", "utf-8");  //設置編碼語言
            connection.setRequestProperty("X-Auth-Token", "token");  //設置請求的token
            connection.setRequestProperty("Connection", "keep-alive");  //設置鏈接的狀態
            connection.setRequestProperty("Transfer-Encoding", "chunked");//設置傳輸編碼
connection.setRequestProperty("Content-Length", obj.toString().getBytes().length + ""); //設置文件請求的長度  

            connection.setReadTimeout(10000);//設置讀取超時時間          
            connection.setConnectTimeout(10000);//設置鏈接超時時間           
            connection.connect();            
            OutputStream out = connection.getOutputStream();//向對象輸出流寫出數據,這些數據將存到內存緩衝區中          
            out.write(obj.toString().getBytes());            //out.write(new String("測試數據").getBytes());            //刷新對象輸出流,將任何字節都寫入潛在的流中       
            out.flush();     
            // 關閉流對象,此時,不能再向對象輸出流寫入任何數據,先前寫入的數據存在於內存緩衝區中          
            out.close();           
            //讀取響應           
            if (connection.getResponseCode()==200)            {
                // 從服務器得到一個輸入流
InputStreamReader inputStream =new InputStreamReader(connection.getInputStream());//調用HttpURLConnection鏈接對象的getInputStream()函數, 將內存緩衝區中封裝好的完整的HTTP請求電文發送到服務端。
BufferedReader reader = new BufferedReader(inputStream);  

        String lines;                
sbuffer= new StringBuffer("");  

          while ((lines = reader.readLine()) != null) {                
            lines = new String(lines.getBytes(), "utf-8");                    
            sbuffer.append(lines);                }                
            reader.close();         
        }else{          
                Log.i(TAG,"請求失敗"+connection.getResponseCode());    
            }    
        //斷開鏈接           
         connection.disconnect();    
     } catch (IOException e) {  
              e.printStackTrace();     
     }   
 }
json數據
public  static String QueryLoginBody(String type,String userid,String checksum){
    String json="{\"type\":\""+type+"\","+"\"jid\":\""+userid+"\","+"\"checkSum\":\""+checksum+"\"}";
    return json;
}
調用方法,輸入要傳入的參數,而後直接把json數據放進去就行了
String json=AppUtils.QueryLoginBody("login","usr","123132");
AppUtils.getUploadInformation("http://www.xxx.com", json);
相關文章
相關標籤/搜索