使用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