使用HttpClient向服務器發送restful post請求


直接上代碼:

public class RestClient {
    public static void main(String[] args) {
        String url = "http://ip地址/api/User/GetToken";
        HttpClient client = new HttpClient();
     //POST方法 PostMethod method
= new PostMethod(url);
     //傳遞參數 NameValuePair[] data
= { new NameValuePair("UserName", "sinopec\\xxxx"), new NameValuePair("Password", "xxxx") }; method.setRequestBody(data); try { int statusCode = client.executeMethod(method); if (statusCode == 200) { String strJson = method.getResponseBodyAsString(); System.out.println(strJson); // System.out.println(map.get("user").getUsername()); } } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM