使用HttpClient向服务器发送restful post请求
2015-12-23 13:02
579 查看
直接上代码:
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(); } } }
相关文章推荐
- 使用HttpURLConnection向服务器发送post和get请求
- Android之Http通信——3.Android HTTP请求方式:HttpURLConnection
- TCP/IP模型是什么
- TCP三次握手的正确使用姿势
- 在Unity3D的网络游戏中实现资源动态加载
- Nginx Parsing HTTP Package、header/post/files/args Sourcecode Analysis
- 易语言注册码网络验证码自制
- TCP/IPillustrated 阅读笔记(一) introduction
- iOS网络收音机
- C#之网络
- 深入浅出HTTP协议(WEB开发和面试必备)
- iOS 网络访问安全问题解决方法App Transport Security policy requires the use of a secure connection
- 网络爬虫——爬取糗事百科笑料段子
- 关于Volley网络请求框架空指针异常问题
- Apache建设虚拟主机httpd-vh…
- [Network Analysis] 复杂网络分析总结
- 【转载】网络
- HTTP笔记(一)基本介绍
- Linux网络编程
- Linux服务器上监控网络带宽的18个常用命令