java.netjava
Get請求android
try { URL url = new URL(""); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; httpConnection.setConnectTimeout(30 * 1000); httpConnection.connect(); if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { //這裏使用的byte[]就至關於iOS中的NSData byte[] buffer = new byte[1024]; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); InputStream inStream = httpConnection.getInputStream(); int len = 0; while ((len = inStream.read(buffer)) != -1) { //inputStream是寫入了內存後的內容,好比從網絡訪問回來的結果或從硬盤中讀取數據。outputStream是從內存中輸出出去的內容,好比輸出到網絡或硬盤。 //這個while循環就是從網絡中按字節爲單位地將數據獲取,inputStream的read是將網絡獲取下來的數據讀到buffer並返回數據字節數。 //若果上次讀入數據成功且是有數據返回,則繼續申請獲取數據,用outStream的write,並將上一次讀入操做獲得的字節數、緩存變量傳入,讓目標源知道此次該返回些什麼數據。 outStream.write(buffer, 0, len); } inStream.close(); System.out.println(new String(outStream.toByteArray())); } httpConnection.disconnect(); } catch (Exception e) { System.out.println("get"+e.getLocalizedMessage()); }
Post請求apache
try { String path = "https://reg.163.com/logins.jsp"; String params = "id=" + URLEncoder.encode("helloworld", "UTF-8") + "&pwd=" + URLEncoder.encode("android", "UTF-8"); // 請求的參數轉換爲byte數組 byte[] postData = params.getBytes(); URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(30 * 1000); // Post請求必須設置容許輸出 urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setRequestMethod("POST"); urlConnection.setInstanceFollowRedirects(true); urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencode"); urlConnection.connect(); //發送請求參數 DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream()); dos.write(postData); dos.flush(); dos.close(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { //xml解析(DOM) InputStream in = urlConnection.getInputStream(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dom = db.parse(in); Element docEle = (Element) dom.getDocumentElement(); NodeList nl = docEle.getElementsByTagName("input"); if (nl != null && nl.getLength()>0) { for (int i = 0; i < nl.getLength(); i++) { Element input = (Element) nl.item(i); String name = input.getAttribute("name"); System.out.println("XML parse excice result : 163's input feild name"); } } //普通字節流獲取 // ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // InputStream inStream = urlConnection.getInputStream(); // int len = 0; // byte[] buffer = new byte[1024]; // while ((len = inStream.read(buffer)) != -1) { // outStream.write(buffer, 0, len); // } // inStream.close(); // System.out.println(new String(outStream.toByteArray())); } } catch (Exception e) { System.out.println("post"+e.getLocalizedMessage()); }
org.apache.http數組
Get請求緩存
try { String path = ""; HttpGet httpGet = new HttpGet(path); HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResp = httpClient.execute(httpGet); if (httpResp.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { String result = EntityUtils.toString(httpResp.getEntity(),"UTF-8"); //JSON解析 String message = new JSONObject(result).getString("message"); System.out.println("HttpGet "+"message:"+message); } } catch (Exception e) { System.out.println("HttpGet "+e.getLocalizedMessage()); }
Post請求網絡
try { String path = ""; HttpPost httpPost = new HttpPost(path); // Post參數 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("id", "helloworld")); params.add(new BasicNameValuePair("pwd", "android")); // 設置字符集 HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); // 設置參數實體 httpPost.setEntity(entity); HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResp = httpClient.execute(httpPost); if (httpResp.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); } } catch (Exception e) { System.out.println("HttpPost "+e.getLocalizedMessage()); }