JAVA代码调用url链接工具类
2015-12-18 13:58
405 查看
作为一个菜鸟,记忆下哥们那的方法,顺便方便以后自己调用,算是自己的成长学习记录
调用一个url链接,返回值为String类型字符串:
必备jar包:
commons-logging-1.1.1.jar
httpclient-4.2.5.jar
httpclient-cache-4.2.5.jar
httpcore-4.2.4.jar
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
public static String get(String url){
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String post(String url){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpget = new HttpPost(url);
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println(HttpClientUtil.post("链接地址"));
}
}
调用一个url链接,返回值为String类型字符串:
必备jar包:
commons-logging-1.1.1.jar
httpclient-4.2.5.jar
httpclient-cache-4.2.5.jar
httpcore-4.2.4.jar
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
public static String get(String url){
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String post(String url){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpget = new HttpPost(url);
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println(HttpClientUtil.post("链接地址"));
}
}
相关文章推荐
- Java 8集合框架源码学习——HashMap
- spring4整合rest风格配置
- Java关键字this、super使用总结
- java 导出exceL
- java实现postgres备份功能
- Java中字节流和字符流的基本认识
- 邮箱组件开发(使用javamail开发)
- JAVA随堂笔记课【五】:包(package)
- java日志组件介绍(common-logging,log4j,slf4j,logback )
- java 定时器Timer
- struts2工作原理
- 自己封装的一个java图片验证码
- 【Java语法】synchronized
- 在eclipse环境下集成开源数据库框架greenDao
- 快速排序-java非递归
- 7 Java Performance Metrics to Watch After a Major Release--转
- Spring_<context:component-scan base-package="">和<context:annotation-config>学习
- Java多线程编程相关资料推荐
- Java基础--package
- Java 7之基础 - 强引用、弱引用、软引用、虚引用