简单的 HttpUtil 在前面的第三方登录里用过
2016-12-09 19:58
267 查看
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpUtil {
public static String post(String url, Map<String, String> paramsMap) {
String result = null;
PostMethod postMethod = null;
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
postMethod = new PostMethod(url);
if (paramsMap != null && paramsMap.size() > 0) {
NameValuePair[] datas = new NameValuePair[paramsMap.size()];
int index = 0;
for (String key : paramsMap.keySet()) {
datas[index++] = new NameValuePair(key, paramsMap.get(key));
}
postMethod.setRequestBody(datas);
}
HttpClientParams httparams = new HttpClientParams();
httparams.setSoTimeout(60000);
postMethod.setParams(httparams);
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK) {
result = postMethod.getResponseBodyAsString();
} else {
System.out.println(" http response status is " + statusCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (postMethod != null) {
postMethod.releaseConnection();
}
}
return result;
}
/*
* 下载APP(包括图片)
*/
public static void httpDownload(String url, String AllFileName) {
HttpClient client = new HttpClient();
GetMethod httpGet = new GetMethod(url);
try {
client.executeMethod(httpGet);
if (httpGet.getResponseHeaders("Content-Disposition").length != 0) {
System.out.println(httpGet
.getResponseHeaders("Content-Disposition")[0]
.getValue());
// fileName = httpGet.getResponseHeaders("Content-Disposition")[0]
// .getValue().split(";")[1].split("\"")[1];
// System.out.println(fileName);
}
// String iconName = fileName + url.substring(url.length() - 4);
InputStream in = httpGet.getResponseBodyAsStream();
FileOutputStream out = new FileOutputStream(new File(AllFileName));
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
//System.out.println("download, success!!");
}
}
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpUtil {
public static String post(String url, Map<String, String> paramsMap) {
String result = null;
PostMethod postMethod = null;
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
postMethod = new PostMethod(url);
if (paramsMap != null && paramsMap.size() > 0) {
NameValuePair[] datas = new NameValuePair[paramsMap.size()];
int index = 0;
for (String key : paramsMap.keySet()) {
datas[index++] = new NameValuePair(key, paramsMap.get(key));
}
postMethod.setRequestBody(datas);
}
HttpClientParams httparams = new HttpClientParams();
httparams.setSoTimeout(60000);
postMethod.setParams(httparams);
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK) {
result = postMethod.getResponseBodyAsString();
} else {
System.out.println(" http response status is " + statusCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (postMethod != null) {
postMethod.releaseConnection();
}
}
return result;
}
/*
* 下载APP(包括图片)
*/
public static void httpDownload(String url, String AllFileName) {
HttpClient client = new HttpClient();
GetMethod httpGet = new GetMethod(url);
try {
client.executeMethod(httpGet);
if (httpGet.getResponseHeaders("Content-Disposition").length != 0) {
System.out.println(httpGet
.getResponseHeaders("Content-Disposition")[0]
.getValue());
// fileName = httpGet.getResponseHeaders("Content-Disposition")[0]
// .getValue().split(";")[1].split("\"")[1];
// System.out.println(fileName);
}
// String iconName = fileName + url.substring(url.length() - 4);
InputStream in = httpGet.getResponseBodyAsStream();
FileOutputStream out = new FileOutputStream(new File(AllFileName));
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
//System.out.println("download, success!!");
}
}
相关文章推荐
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
- 最简单的moss单点登录第三方系统,有点非主流
- iOS 类似简单的第三方微博客户端,可进行登录微博浏览相关信息
- iOS 微博第三方登录的简单实现
- iOS 微信第三方登录的简单实现
- 简单的第三方登录之QQ
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
- iOS QQ第三方登录的简单实现
- 简单的faceBook第三方登录Demo
- [简单实用系列] 微博第三方登录
- android 简单实现,微信第三方登录
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
- [简单实用系列] 微信第三方登录
- 第三方登录facebook登录简单集成iOS APP超详细教程
- 单点登录在ASP.NET上的简单实现
- 用JSP+Servlet+JavaBean模式实现一个简单的登录网页设计(JSP+Tomcat+MySQL)
- li的简单应用(将前面的点换成图标)
- 在web中实现简单的注册,登录功能
- 简单示例:AJAX结合PHP代码实现登录