代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
HttpClient工具,使用Apache的HttpClient类实现get和p
作者:
/ 发布于
2017/9/27
/
674
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class HttpUtil { public static String httpGet(String url) { String response = null; try { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); if (null != httpResponse) { int status = httpResponse.getStatusLine().getStatusCode(); if (HTTP_OK == status) { HttpEntity httpEntity = httpResponse.getEntity(); response = EntityUtils.toString(httpEntity, HTTP.UTF_8); } else { response = ("错误码:" + status); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } public static String httpPost(String url, List<HttpPostParam> params) { String response = null; try { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); if (null != params) { if (params.size() > 0) { List<NameValuePair> pairsList = new ArrayList<NameValuePair>(); for (int i = 0; i < params.size(); i++) { HttpPostParam param = params.get(i); NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue()); pairsList.add(pair); } httpPost.setEntity(new UrlEncodedFormEntity(pairsList, HTTP.UTF_8)); } } HttpResponse httpResponse = httpClient.execute(httpPost); if (null != httpResponse) { int status = httpResponse.getStatusLine().getStatusCode(); if (HTTP_OK == status) { HttpEntity httpEntity = httpResponse.getEntity(); response = EntityUtils.toString(httpEntity, HTTP.UTF_8); } else { response = ("错误码:" + status); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); }catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return response; } private final static int HTTP_OK = 200; }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3