1 package com.lyl.interfaces;
 2  
 3 import java.net.URI;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6  
 7 import org.testng.annotations.Test;
 8 import org.apache.http.HttpResponse;
 9 import org.apache.http.client.HttpClient;
10 import org.apache.http.client.entity.UrlEncodedFormEntity;
11 import org.apache.http.client.methods.HttpGet;
12 import org.apache.http.client.methods.HttpPost;
13 import org.apache.http.client.utils.URLEncodedUtils;
14 import org.apache.http.impl.client.HttpClients;
15 import org.apache.http.message.BasicNameValuePair;
16 import org.apache.http.util.EntityUtils;
17 import org.apache.log4j.Logger;
18  
19 public class Demo {
20     //日志
21     private Logger logger=Logger.getLogger(Demo.class);
22     @Test(enabled=false)
23     public void testLoginPost() {
24         //准备url
25         String url = "http://******/api/member/login";
26         //选择请求方式
27         HttpPost httppost = new HttpPost(url);
28         //准备参数
29         List<BasicNameValuePair> para=new ArrayList<BasicNameValuePair>();
30         BasicNameValuePair mobliePhone=    new BasicNameValuePair("mobilephone","**********");
31         BasicNameValuePair pwd=new BasicNameValuePair("password", "e10adc3949ba59abbe56e057f20f883e");
32         BasicNameValuePair type=new BasicNameValuePair("type", "1");
33         para.add(mobliePhone);
34         para.add(pwd);
35         para.add(type);
36         try {//把参数封装到请求体里面
37             httppost.setEntity(new UrlEncodedFormEntity(para,"UTF-8"));
38             //准备客户端
39             HttpClient httpclient =HttpClients.createDefault();
40             //发送请求,接受响应信息
41             HttpResponse respones=httpclient.execute(httppost);
42             //取出接口响应数据,并转成字符串
43             String result=  EntityUtils.toString(respones.getEntity());
44             System.out.println(result);
45         } catch (Exception e) {
46             // TODO Auto-generated catch block
47             logger.error("post请求出问题啦");
48         }
49     }
50     
51     @Test
52     public void testLoginGet(){
53                 //准备url
54                 String url = "http://*******/api/member/login";
55                 //选择请求方式
56                 HttpGet httpget = new HttpGet(url);
57                 //准备参数
58                 List<BasicNameValuePair> para=new ArrayList<BasicNameValuePair>();
59                 BasicNameValuePair mobliePhone=    new BasicNameValuePair("mobilephone","*****");
60                 BasicNameValuePair pwd=new BasicNameValuePair("password", "e10adc3949ba59abbe56e057f20f883e");
61                 BasicNameValuePair type=new BasicNameValuePair("type", "1");
62                 para.add(mobliePhone);
63                 para.add(pwd);
64                 para.add(type);
65                 String paramers=URLEncodedUtils.format(para, "UTF-8");
66                 //将参数拼接在url上面
67                 url+=("?"+paramers);
68                 try {
69                     httpget.setURI(new URI(url));
70                     //准备客户端
71                     HttpClient httpclient=HttpClients.createDefault();
72                     //执行
73                     HttpResponse response=httpclient.execute(httpget);
74                     String result=EntityUtils.toString(response.getEntity());
75                 } catch (Exception e) {
76                     // TODO Auto-generated catch block
77                     logger.error("get请求出问题啦");
78                 }
79     }
80 }
81 --------------------- 
82 作者:lyl00ling 
83 来源:CSDN 
84 原文:https://blog.csdn.net/lyl00ling/article/details/80311776 
85 版权声明:本文为博主原创文章,转载请附上博文链接!