实战项目跟敲日记 day-11
这一天的课程没碰到什么问题,就是虚拟机上搭建的图片服务器出了问题,一些文件诡异的失踪了,重新安装下就好了。
今天的部分收货
1.知道了RESTClient这个好用的软件。
2.对Httpclient有了了解能简单使用
在服务器上模拟浏览器做http请求,并能将这些请求结果提取出来,自己没写注释(代码逻辑太清楚了),直接拷的示例代码。
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建http GET请求
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("内容长度:" + content.length());
// FileUtils.writeStringToFile(new File("C:\\baidu.html"), content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
带参数:
URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
System.out.println(uri);
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
post:
// 创建http POST请求
HttpPost httpPost = new HttpPost("http://www.oschina.net/");
post 带参
// 设置2个post参数,一个是scope、一个是q
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("scope", "project"));
parameters.add(new BasicNameValuePair("q", "java"));
// 构造一个form表单式的实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
// 将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
今天没碰到什么问题,主要工作都是实现各种单表查询,返回数据,看实战项目中系统怎么实现,今天状态太差,又写任务书又开半天会。。。先休息明天再努力 太敷衍了今天。。定个小目标,明天把一天的视频课看完,20道java面试题