package com.http.test;
import java.io.File;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.MultipartPostMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
public class HttpClientUtile {
public String sendMail(String url) {
String strResponse = "";
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建POST方法的实例
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = { new NameValuePair("sign", "agsend"),
new NameValuePair("From", "EMAIL"),
new NameValuePair("to", "to"),
new NameValuePair("subject", "subject"),
new NameValuePair("body", "body"),
new NameValuePair("password", "PASSWORD") };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
try {
httpClient.getParams().setContentCharset("GBK");
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
// if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
// || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// String responseBody = postMethod.getResponseBodyAsString();
// log.info(responseBody);
// if (responseBody != null) {
// if ("succ".equals(responseBody)) { // 发送成功
//
// isSended = true;
// }
// }
// }
if (statusCode == 200) {
strResponse = postMethod.getResponseBodyAsString();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
postMethod.releaseConnection();
}
return strResponse;
}
/**
* @category 此方法经过验证行不通,就是上传文件和普通的域提交不能同时进行,请各位再加实验
* @param strUrl
* @param strFrom
* @param strTo
* @param strTitle
* @param strBody
* @param strFiles
* @return
*/
public String sendMail(String strUrl,String strFrom,String strTo ,String strTitle,String strBody,String strFiles)
{
String strResponse = null;
HttpClient httpClient = new HttpClient();
MultipartPostMethod multipartPostMethod = new MultipartPostMethod(strUrl);
try {
multipartPostMethod.addParameter("from",strFrom);
multipartPostMethod.addParameter("to",strTo);
multipartPostMethod.addParameter("title",strTitle);
multipartPostMethod.addParameter("body",strBody);
String strFileName = "aaa";
if (strFiles.indexOf("/")>0) {
String[] strTmp = strFiles.split("/");
strFileName = strTmp[strTmp.length-1];
}
if (strFiles.indexOf("\\\\")>0) {
String[] strTmp = strFiles.split("\\\\");
strFileName = strTmp[strTmp.length-1];
}
multipartPostMethod.addParameter("file",strFileName,new File(strFiles));
multipartPostMethod.addPart(new FilePart(strFileName,new File(strFiles)));
//httpClient.getParams().setContentCharset("UTF-8");
PostMethod postMethod = new PostMethod(strUrl);
NameValuePair[] data = {
new NameValuePair("from", "EMAIL"),
new NameValuePair("to", "to"),
new NameValuePair("title", "subject"),
new NameValuePair("body", "body") };
postMethod.setRequestBody(data);
//int statusCode = httpClient.executeMethod(postMethod);
int statusCode = httpClient.executeMethod(multipartPostMethod);
if (statusCode == 200) {
strResponse = multipartPostMethod.getResponseBodyAsString();
}
} catch (Exception e){
e.printStackTrace();
}
finally
{
multipartPostMethod.releaseConnection();
}
return strResponse;
}
public String uploadFiles(String strUrl, String[] strFiles)
{
String strResponse = null;
HttpClient httpClient = new HttpClient();
MultipartPostMethod multipartPostMethod = new MultipartPostMethod(strUrl);
try {
for (int i = 0; i < strFiles.length; i++) {
multipartPostMethod.addPart(new FilePart("file"+i,new File(strFiles[i])));
}
int statusCode = httpClient.executeMethod(multipartPostMethod);
if (statusCode == 200) {
strResponse = multipartPostMethod.getResponseBodyAsString();
}
} catch (Exception e){
e.printStackTrace();
}
finally
{
multipartPostMethod.releaseConnection();
}
return strResponse;
}
public String uploadFiles(String strUrl,String strFrom, String strFiles)
{
String strResponse = null;
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(strUrl);
try {
FilePart filePart = new FilePart("file","aa.txt",new File(strFiles));
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(new Part[]{new StringPart("HIDDEN","from",strFrom),filePart},postMethod.getParams());
postMethod.addParameter(new NameValuePair("from",strFrom));
postMethod.setRequestEntity(multipartRequestEntity);
httpClient.getParams().setContentCharset("UTF-8");
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
strResponse = postMethod.getResponseBodyAsString();
}
} catch (Exception e){
e.printStackTrace();
}
finally
{
postMethod.releaseConnection();
}
return strResponse;
}
/**
* @category 上传附件
* @param strUrl
* @param strFiles
* @return 返回状态码 200 正常
* @throws Exception
*/
public String uploadFiles(String strUrl,String attachments) throws Exception
{
String strResponse = null;
HttpClient httpClient = new HttpClient();
MultipartPostMethod postMethod = new MultipartPostMethod(strUrl.trim());
try {
if (null != attachments && attachments.trim().length()>0) {
String [] strArr = attachments.trim().split("\\|");
for (int i = 0; i < strArr.length; i++) {
if (null!= strArr[i] && strArr[i].trim().length()>0) {
String fileName = strArr[i].trim();
fileName = fileName.replaceAll("\\\\", "/").replaceAll("//","/");
fileName = fileName.replaceAll("\\\\", "/").replaceAll("//","/");
String[] strTmpNameArr = fileName.split("/");
if (strTmpNameArr.length>0) {
fileName = strTmpNameArr[strTmpNameArr.length-1];
postMethod.addPart(new FilePart(fileName,new File(strArr[i].trim())));
}
}
}
}
else {
return "File can not be null";
}
httpClient.getParams().setContentCharset("UTF-8");
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
strResponse = postMethod.getResponseBodyAsString();
}
return strResponse;
} catch (Exception e){
throw e;
}
finally
{
postMethod.releaseConnection();
}
}
/**
* @category 发送邮件信息
* @param strUrl
* @param sysID
* @param pass
* @param from
* @param to
* @param title
* @param level
* @param body
* @param attachments
* @return 正常返回 200
* @throws Exception
*/
public String postMessage(String strUrl,String sysID,String pass,String from,String to,String title,String level,String body,String attachments) throws Exception
{
HttpClient httpClient = new HttpClient();
String responseBody="";
PostMethod postMethod = new PostMethod(strUrl.trim());
try {
NameValuePair[] nameValuePairs ={new NameValuePair("SysID",sysID),
new NameValuePair("Pass",pass),new NameValuePair("From",from),
评论23
最新资源