Home  >  Article  >  Java  >  Java implementation of get, PUT, POST, delete requests

Java implementation of get, PUT, POST, delete requests

巴扎黑
巴扎黑Original
2017-03-28 15:36:172292browse

Java implementation of get, PUT, POST, delete request:

1, get

public static String doGet(String strUrl ){
    String strReturn=""; 
    HttpGet httpGet = new HttpGet(strUrl);
    CloseableHttpClient httpclient = null; 
    CloseableHttpResponse response1=null;
        try {
        httpclient = HttpClients.createDefault();
        response1 = httpclient.execute(httpGet); 
            HttpEntity entity1 = response1.getEntity(); 
            strReturn=EntityUtils.toString(entity1) ;
            EntityUtils.consume(entity1); 
        }catch(Exception e){ 
        e.printStackTrace();
        }finally {  
try {
if(response1!=null)
response1.close();
} catch (IOException e) { 
e.printStackTrace();
}
        }
    return strReturn;
    }

2, put

public static String doPut(String strUrl,String param){ 
     CloseableHttpClient httpclient = HttpClients.createDefault();
     StringBuffer jsonString= new StringBuffer();
         try {
         final HttpPut put=new HttpPut(strUrl);
         put.setEntity(new StringEntity(param,"UTF-8")); 
            CloseableHttpResponse response1= httpclient.execute(put ); 
             try {
                 HttpEntity entity1 = response1.getEntity();
                 BufferedReader br = new BufferedReader(new InputStreamReader(entity1.getContent())); 
                 String line;
                 while ((line = br.readLine()) != null) {
                         jsonString.append(line);
                 }  
                 
                 EntityUtils.consume(entity1);
             } finally {
                 response1.close();
             }
         }catch(Exception e){
         e.printStackTrace();
         }
         return jsonString.toString();
    }

3, post

public static String doPost(String requestUrl, String payload) {
    String strReturn="";  
   PostMethod httpost = new PostMethod(requestUrl);   
   httpost.setRequestBody(payload);  
try { 
       httpClient.executeMethod(httpost);
       byte[] bytes = httpost.getResponseBody(); 
       strReturn=  new String(bytes) ; 
} catch (Exception e) { 
e.printStackTrace();
}
    return strReturn;
   }

4, delete

public static void doDelete(String urlToRead) throws Exception {
   URL url = new URL(urlToRead);
   HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
   httpCon.setDoOutput(true);
   httpCon.setRequestProperty(
       "Content-Type", "application/x-www-form-urlencoded" );
   httpCon.setRequestMethod("DELETE");
   httpCon.connect();
   httpCon.disconnect(); 
        
    }

The above is the Java implementation, PUT, POST, delete the requested content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related articles:

Detailed introduction to Java programming common problems summary

Detailed explanation of Java multi-threading basics

Graphic introduction to Java remote communication technology and principle analysis

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Previous article:List sortingNext article:List sorting