Java网络通信之HttpClient

这里我用的org.apache的包,closeableHttpClient是实现了HttpClient接口的子类。服务器需要自己去搭建。下个tomcat搭建服务器吧

一.Post方式

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Administrator on 2016/7/26.
*/
public class HttpTestDemo {
public static void main(String[] args) throws IOException {
// get();
post();
}

/**
 * post请求
 */
private static void post() throws IOException {
    CloseableHttpClient mCloseableHttpClient= HttpClients.createDefault();//创建默认的客户端 相当于UrlConnection 里面的URL
    HttpPost mHttpPost = new HttpPost("http://localhost:8080/day14servletdemo/DServlet");//创建post请求的封装类
    List<NameValuePair> mList = new ArrayList<>();//创建存放参数的集合
    mList.add(new BasicNameValuePair("name","gdsgsdf"));//添加参数
    mList.add(new BasicNameValuePair("password","fgdsgdsfg"));
    mHttpPost.setEntity(new UrlEncodedFormEntity(mList));//使用url编码的参数 ,里面就是把键值对取出来然后拼接成post请求需要的格式
    //正式请求
    CloseableHttpResponse mCloseableHttpResponse =mCloseableHttpClient.execute(mHttpPost);//连接获取返回的数据
    int code= mCloseableHttpResponse.getStatusLine().getStatusCode();//获取错误码
    if (code==200){
        //获取数据
        InputStream mInputStream= mCloseableHttpResponse.getEntity().getContent();
        BufferedReader mBufferedReader= new BufferedReader(new InputStreamReader(mInputStream));//包装一个字符流
        String line = null;
        while ((line=mBufferedReader.readLine())!=null){//读流
            System.out.println(line);//输出结果
        }
    }
}

##二.Get方式

/**
* 使用httpclient 的get方式请求网络
* 网络请求来说 : 主机名(访问地址) 参数 事先设置好,然后通过某种方式会发起请求
*/
private static void get() {
try {
CloseableHttpClient mCloseableHttpClient= HttpClients.createDefault();//创建默认的客户端 相当于UrlConnection 里面的URL
HttpGet mHttpGet = new HttpGet(“http://localhost:8080/day14servletdemo/DServlet?name=gdsgsdf&password=gdsfgdg&sex=0&habit=smoke&habit=drink&habit=tangtou&edu=01&desc=gdfsgdsfgdg“);
CloseableHttpResponse mCloseableHttpResponse= mCloseableHttpClient.execute(mHttpGet);//发起get请求,并且获取返回的数据
if (mCloseableHttpResponse.getStatusLine().getStatusCode()== 200){//如果返回的错误码(状态码)为200 代表响应成功 返回数据
//获取数据
InputStream mInputStream= mCloseableHttpResponse.getEntity().getContent();
BufferedReader mBufferedReader= new BufferedReader(new InputStreamReader(mInputStream));//包装一个字符流
String line = null;
while ((line=mBufferedReader.readLine())!=null){//读流
System.out.println(line);//输出结果
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
“`

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值