欢迎来到Introzo百科
Introzo百科
当前位置:网站首页 > 技术 > HttpClient get和HttpClient Post请求的方式获取服务器的返回数据

HttpClient get和HttpClient Post请求的方式获取服务器的返回数据

日期:2023-09-16 08:35

-->

1.转自:https://www.introzo.com/alinshen/article/details/78221567?utm_source=blogxgwz4

/*
 * 演示通过HttpClient get请求的方式获取服务器的返回数据
 */

  1. public class HttpClientDemo {
  2. public static void main(String[] args) throws ClientProtocolException, IOException {
  3. String path="http://10.0.184.105:58080/ServletDemo4/LoginServlet?username=admin&password=admin";
  4. //1.创建客户端访问服务器的httpclient对象   打开浏览器
  5. HttpClient httpclient=new DefaultHttpClient();
  6. //2.以请求的连接地址创建get请求对象     浏览器中输入网址
  7. HttpGet httpget=new HttpGet(path);
  8. //3.向服务器端发送请求 并且获取响应对象  浏览器中输入网址点击回车
  9. HttpResponse response=httpclient.execute(httpget);
  10. //4.获取响应对象中的响应码
  11. StatusLine statusLine=response.getStatusLine();//获取请求对象中的响应行对象
  12. int responseCode=statusLine.getStatusCode();//从状态行中获取状态码
  13. if(responseCode==200){
  14. //5.获取HttpEntity消息载体对象  可以接收和发送消息
  15. HttpEntity entity=response.getEntity();
  16. //EntityUtils中的toString()方法转换服务器的响应数据
  17. String str=EntityUtils.toString(entity, "utf-8");
  18. System.out.println("服务器的响应是:"+str);
  19. //          //6.从消息载体对象中获取操作的读取流对象
  20. //          InputStream input=entity.getContent();
  21. //          BufferedReader br=new BufferedReader(new InputStreamReader(input));
  22. //          String str=br.readLine();
  23. //          String result=new String(str.getBytes("gbk"), "utf-8");
  24. //          System.out.println("服务器的响应是:"+result);
  25. //          br.close();
  26. //          input.close();
  27. }else{
  28. System.out.println("响应失败!");
  29. }
  30. }
  31. }

/*
 * 演示HttpClient使用Post提交方式提交数据
 *


 *  
 *  
 *

 * 
 *  username=输入值   password=输入值
 */

    1. public class HttpClientDemo4 {
    2. public static void main(String[] args) throws ClientProtocolException, IOException {
    3. String baseUrl="http://10.0.184.105:58080/ServletDemo4/LoginServlet";//username=? password=?
    4. HttpClient httpclient=new DefaultHttpClient();
    5. //以请求的url地址创建httppost请求对象
    6. HttpPost httppost=new HttpPost(baseUrl);
    7. //NameValuePair 表示以类的形式保存提交的键值对
    8. NameValuePair pair1=new BasicNameValuePair("username", "ad");
    9. NameValuePair pair2=new BasicNameValuePair("password", "admin");
    10. //集合的目的就是存储需要向服务器提交的key-value对的集合
    11. List listPair=new ArrayList();
    12. listPair.add(pair1);
    13. listPair.add(pair2);
    14. //HttpEntity 封装消息的对象 可以发送和接受服务器的消息  可以通过客户端请求或者是服务器端的响应获取其对象
    15. HttpEntity entity=new UrlEncodedFormEntity(listPair);//创建httpEntity对象
    16. httppost.setEntity(entity);//将发送消息的载体对象封装到httppost对象中
    17. HttpResponse response=httpclient.execute(httppost);
    18. int responseCode=response.getStatusLine().getStatusCode();
    19. if(responseCode==200){
    20. //得到服务器响应的消息对象
    21. HttpEntity httpentity=response.getEntity();
    22. System.out.println("服务器响应结果是:"+EntityUtils.toString(httpentity, "utf-8"));
    23. }else{
    24. System.out.println("响应失败!");
    25. }
    26. }
    27. }
-->