HttpURLConnection和HttpClient

來源:互聯網
上載者:User

標籤:

URLConnection

String urlAddress = "http://192.168.1.102:8080/AndroidServer/login.do";      URL url;      HttpURLConnection uRLConnection;      public UrlConnectionToServer(){        }      //向伺服器發送get請求    public String doGet(String username,String password){          String getUrl = urlAddress + "?username="+username+"&password="+password;          try {              url = new URL(getUrl);              uRLConnection = (HttpURLConnection)url.openConnection();              InputStream is = uRLConnection.getInputStream();              BufferedReader br = new BufferedReader(new InputStreamReader(is));              String response = "";              String readLine = null;              while((readLine =br.readLine()) != null){                  //response = br.readLine();                  response = response + readLine;              }              is.close();              br.close();              uRLConnection.disconnect();              return response;          } catch (MalformedURLException e) {              e.printStackTrace();              return null;          } catch (IOException e) {              e.printStackTrace();              return null;          }      }            //向伺服器發送post請求    public String doPost(String username,String password){          try {              url = new URL(urlAddress);              uRLConnection = (HttpURLConnection)url.openConnection();              uRLConnection.setDoInput(true);              uRLConnection.setDoOutput(true);              uRLConnection.setRequestMethod("POST");              uRLConnection.setUseCaches(false);              uRLConnection.setInstanceFollowRedirects(false);              uRLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");              uRLConnection.connect();                            DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream());              String content = "username="+username+"&password="+password;              out.writeBytes(content);              out.flush();              out.close();                            InputStream is = uRLConnection.getInputStream();              BufferedReader br = new BufferedReader(new InputStreamReader(is));              String response = "";              String readLine = null;              while((readLine =br.readLine()) != null){                  //response = br.readLine();                  response = response + readLine;              }              is.close();              br.close();              uRLConnection.disconnect();              return response;          } catch (MalformedURLException e) {              e.printStackTrace();              return null;          } catch (IOException e) {              e.printStackTrace();              return null;          }      }  

 

 

HTTPClient

 

String urlAddress = "http://192.168.1.102:8080/qualityserver/login.do";  public HttpClientServer(){             }        public String doGet(String username,String password){      String getUrl = urlAddress + "?username="+username+"&password="+password;      HttpGet httpGet = new HttpGet(getUrl);      //建立get方法執行個體    HttpParams hp = httpGet.getParams();       //設定參數    hp.getParameter("true");      //hp.      //httpGet.setp      HttpClient hc = new DefaultHttpClient();     //單例模式,類似於只有一個標籤頁的瀏覽器    try {          HttpResponse ht = hc.execute(httpGet);    //執行get方法獲得http執行個體對象        if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){       //可以直接寫200, SC_OK = State Code OK            HttpEntity he = ht.getEntity();       // 獲得實體,BasicHttpEntity這個類就是一個輸入資料流的內容封裝類,封裝內容的相關的編碼格式,長度等             InputStream is = he.getContent();  //獲得實體的內容,即響應資料            BufferedReader br = new BufferedReader(new InputStreamReader(is));              String response = "";              String readLine = null;              while((readLine =br.readLine()) != null){                  //response = br.readLine();                  response = response + readLine;              }                          br.close(); 
is.close();
//String str = EntityUtils.toString(he); System.out.println("========="+response); return response; }else{ return "error"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } }

public String doPost(String username,String password){ //String getUrl = urlAddress + "?username="+username+"&password="+password; HttpPost httpPost = new HttpPost(urlAddress); //建立post方法執行個體 List params = new ArrayList(); NameValuePair pair1 = new BasicNameValuePair("username", username); //名稱值對,POST方法中常見 NameValuePair pair2 = new BasicNameValuePair("password", password); params.add(pair1); params.add(pair2); HttpEntity he; try { he = new UrlEncodedFormEntity(params, "gbk"); httpPost.setEntity(he); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpClient hc = new DefaultHttpClient(); try { HttpResponse ht = hc.execute(httpPost); //串連成功 if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity het = ht.getEntity(); InputStream is = het.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); //String str = EntityUtils.toString(he); System.out.println("=========&&"+response); return response; }else{ return "error"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } }

 

(摘自:http://www.cnblogs.com/devinzhang/archive/2012/01/17/2325092.html)

 

HttpURLConnection和HttpClient

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.