HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
来源:互联网 发布:变色龙引导u盘安装mac 编辑:程序博客网 时间:2024/05/02 04:56
/*
* 演示通过HttpClient get请求的方式获取服务器的返回数据
*/
public class HttpClientDemo {public static void main(String[] args) throws ClientProtocolException, IOException {String path="http://10.0.184.105:58080/ServletDemo4/LoginServlet?username=admin&password=admin";//1.创建客户端访问服务器的httpclient对象 打开浏览器HttpClient httpclient=new DefaultHttpClient();//2.以请求的连接地址创建get请求对象 浏览器中输入网址HttpGet httpget=new HttpGet(path);//3.向服务器端发送请求 并且获取响应对象 浏览器中输入网址点击回车HttpResponse response=httpclient.execute(httpget);//4.获取响应对象中的响应码StatusLine statusLine=response.getStatusLine();//获取请求对象中的响应行对象int responseCode=statusLine.getStatusCode();//从状态行中获取状态码if(responseCode==200){//5.获取HttpEntity消息载体对象 可以接收和发送消息HttpEntity entity=response.getEntity();//EntityUtils中的toString()方法转换服务器的响应数据String str=EntityUtils.toString(entity, "utf-8");System.out.println("服务器的响应是:"+str);////6.从消息载体对象中获取操作的读取流对象//InputStream input=entity.getContent();//BufferedReader br=new BufferedReader(new InputStreamReader(input));//String str=br.readLine();//String result=new String(str.getBytes("gbk"), "utf-8");//System.out.println("服务器的响应是:"+result);//br.close();//input.close();}else{System.out.println("响应失败!");}}}
/*
* 演示HttpClient使用Post提交方式提交数据
* <form action="" method="post">
* <input type="text" name="username" value="输入值">
* <input type="password" name="password" value="输入值">
* </form>
*
* username=输入值 password=输入值
*/
public class HttpClientDemo4 {public static void main(String[] args) throws ClientProtocolException, IOException {String baseUrl="http://10.0.184.105:58080/ServletDemo4/LoginServlet";//username=? password=?HttpClient httpclient=new DefaultHttpClient();//以请求的url地址创建httppost请求对象HttpPost httppost=new HttpPost(baseUrl);//NameValuePair 表示以类的形式保存提交的键值对NameValuePair pair1=new BasicNameValuePair("username", "ad");NameValuePair pair2=new BasicNameValuePair("password", "admin");//集合的目的就是存储需要向服务器提交的key-value对的集合List<NameValuePair> listPair=new ArrayList<NameValuePair>();listPair.add(pair1);listPair.add(pair2);//HttpEntity 封装消息的对象 可以发送和接受服务器的消息 可以通过客户端请求或者是服务器端的响应获取其对象HttpEntity entity=new UrlEncodedFormEntity(listPair);//创建httpEntity对象httppost.setEntity(entity);//将发送消息的载体对象封装到httppost对象中HttpResponse response=httpclient.execute(httppost);int responseCode=response.getStatusLine().getStatusCode();if(responseCode==200){//得到服务器响应的消息对象HttpEntity httpentity=response.getEntity();System.out.println("服务器响应结果是:"+EntityUtils.toString(httpentity, "utf-8"));}else{System.out.println("响应失败!");}}}
0 0
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- HttpClient的get和post请求数据
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Httpclient的Get请求和Post请求
- HttpClient的Get请求和Post请求
- httpclient的get和post请求
- HttpClient 的post和get请求
- httpClient的Get和Post请求
- HttpClient的post和get请求
- httpclient的get和post请求
- HttpClient的GET和POST请求
- HttpClient--HttpClient的Get请求方式
- HttpClient请求服务器 get post
- HTTPClient get post 请求方式
- HttpClient Post、get 请求方式
- 树的非递归深度优先遍历
- 2015年1月26日 星期一 [iOS]UIwebview左右滑動回上一頁
- poj1742 dp
- CoreText 使用教程:以创建一个简单的杂志应用为例
- 二叉树的建立与遍历-数组模拟和链表模拟
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- CFNetwork概念(CFNetwork Concepts )
- 开始学习C语言了
- javascript 运动函数
- ARM + OpenCV 抓取摄像头图像 && 一点感想
- Java JDK的环境变量配置
- Qtcreator使用中遇到的错误汇总
- B\S备忘录04——接下来进入迂回路线,先发个邮件短信看看
- STL系列---全排列(百度迅雷笔试题)