解决方案 »
- DW中做好的页面,拷贝到Myeclipse里 css样式没效果, 菜niao问题求助
- 用了input type="file"的click方法后,submit必须点击两次才能提交
- 使用Bakit对SVG编程时,如何从svg文件里获得ellipse结点的位置
- 用了struts,<body background="img/right.jpg"> 没有效果了?
- 每两条一行显示数据
- 关于getParameter返回值问题
- JSP调试的问题
- jsp页面向access数据库中插入纪录汉字时乱码(???ê?¤)?请大虾帮忙!
- 哪里可以找到tomcat3.1?
- JPA批量插入数据求解
- 关闭statement时出现NullPointerException
- strtus2配置问题
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
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.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import com.deelon.model.W_self_accounts;public class WebRuquest {
static Log Logger = LogFactory.getLog(WebRuquest.class);
private static HttpClient client; public static HttpClient getClient() {
if (client == null) {
client = new HttpClient();
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
client.getParams().getDefaults().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
client.getParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
}
return client;
} public static String post(String url, List<NameValuePair> heads, String cookies, String str) {
return post(url, heads, cookies);
} public static synchronized String post(String url, List<NameValuePair> heads, String cookies) {
Logger.info(url);
PostMethod post = new PostMethod(url);
try {
NameValuePair[] a = new NameValuePair[0];
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
if (null != heads) {
a = new NameValuePair[heads.size()];
heads.toArray(a);
}
post.addParameters(a);
if (StringUtils.isBlank(cookies) && StringUtils.isEmpty(cookies)) {
post.addRequestHeader("cookie", cookies);
}
int statusCode = getClient().executeMethod(post);
String entity = "";
Header head = post.getResponseHeader("Set-cookie");
Logger.info("post:" + head);
if (statusCode == HttpStatus.SC_OK) {
entity = post.getResponseBodyAsString();
}
Logger.info(entity);
showCookie("");
return entity;
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
return null;
} public static synchronized String get(String url, W_self_accounts w_self_accounts) {
GetMethod get = new GetMethod(url);
try {
if (null != w_self_accounts) {
if (null != w_self_accounts.getCookies()) {
for (int i = 0; i < w_self_accounts.getCookies().size(); i++) {
get.addRequestHeader(w_self_accounts.getCookies().get(i).getName(), w_self_accounts.getCookies().get(i).getValue());
}
}
if (StringUtils.isBlank(w_self_accounts.getCookieStr()) && StringUtils.isEmpty(w_self_accounts.getCookieStr())) {
get.addRequestHeader("cookice", w_self_accounts.getCookieStr());
}
}
get.addRequestHeader("cookie", "U_TRS1=00000021.8537292e.504d708e.0e241dbb; path=/; expires=Thu, 08-Sep-22 04:46:06 GMT; domain=.sina.com.cn, U_TRS2=00000021.8546292e.504d708e.25b18021; path=/; domain=.sina.com.cn");
int statusCode = getClient().executeMethod(get);
String entity = "";
Logger.info(statusCode);
if (statusCode == HttpStatus.SC_OK) {
entity = get.getResponseBodyAsString();
}
// Logger.info(get.getResponseBodyAsString());
// Header head = get.getResponseHeader("Set-cookie");
// Logger.info(head);
// showCookie();
return entity;
} catch (Exception e) {
e.printStackTrace();
} finally {
get.releaseConnection();
}
return null;
} /**
* ge
*
* @param url
* @param w_self_accounts
* @param list
* @return
*/
public static synchronized String getOfCookie(String url, W_self_accounts w_self_accounts) {
GetMethod get = new GetMethod(url);
try {
if (null != w_self_accounts.getCookies()) {
for (int i = 0; i < w_self_accounts.getCookies().size(); i++) {
get.addRequestHeader(w_self_accounts.getCookies().get(i).getName(), w_self_accounts.getCookies().get(i).getValue());
}
}
int statusCode = getClient().executeMethod(get);
String entity = "";
if (statusCode == HttpStatus.SC_OK) {
entity = get.getResponseBodyAsString();
}
// Logger.info(get.getResponseBodyAsString());
Header head = get.getResponseHeader("Set-cookie");
w_self_accounts.setCookieStr(head.getValue());
Logger.info(head);
Logger.info("--------------------");
showCookie("");
Logger.info("===========================================");
return entity;
} catch (Exception e) {
e.printStackTrace();
} finally {
get.releaseConnection();
}
return null;
} public static String UploadFile(String url, String filePath, String formFieldName, String contentType, String cookies, String referer) {
String str = "";
PostMethod post = null;
try {
java.io.File info = new java.io.File(filePath);
if (!info.exists()) {
Logger.debug("无法上传图片,找不到文件" + info);
return str;
}
Logger.debug(String.format("Uploading {0} {1}", filePath, url));
String str2 = "\r\n-----------------------------" + System.currentTimeMillis() + "\r\n";
byte[] bytes = str2.getBytes("ASCII");
// ;Encoding.ASCII.GetBytes("\r\n--" + str2 + "\r\n");
post = new PostMethod(url);
if (StringUtils.isBlank(cookies) && StringUtils.isEmpty(cookies)) {
post.addRequestHeader("cookice", cookies);
}
Part[] parts = {new FilePart(info.getName(), info) };
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
// post.addRequestHeader(new Header().)
int statuscode = getClient().executeMethod(post);
if (statuscode == HttpStatus.SC_OK) {
Logger.info("上传成功");
str = post.getRequestHeader("Location").getValue();
}else{
Logger.info("上传失败! 状态吗:" + statuscode);
}
Header head = post.getRequestHeader("Set-Cookie");
Logger.info("upload:" + head.getValue());
// showCookie();
} catch (Exception exception) {
Logger.info(exception);
Logger.info("图片上传失败:" + exception.getMessage());
} finally {
if (null != post) {
post.releaseConnection();
}
}
Logger.debug("Header Location value:" + str);
if (!StringUtils.isBlank(str) && !StringUtils.isEmpty(str)) {
int index = str.indexOf("&pid=");
if (index != -1) {
str = str.substring(index + "&pid=".length());
str = str.substring(0, str.indexOf("&"));
}
}
return str;
} private static void showCookie(String str) {
// 查看 cookie 信息
Logger.info("------------------getClient().getState().getCookies();-----------------");
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
// getClient().getState().setC
Cookie[] cookies= getClient().getState().getCookies();
for (int i = 0; i < cookies.length; i++) {
Logger.info(cookies[i]);
}
Logger.info("----------------end--getClient().getState().getCookies();-----------------");
Logger.info("------------------weibocookies-----------------");
Cookie[] weibocookies = cookiespec.match(Contants.LOGON_SITE, Contants.LOGON_PORT, "/", false, client.getState().getCookies());
if (weibocookies.length == 0) {
Logger.info("无weibocookies");
} else {
for (int i = 0; i < weibocookies.length; i++) {
Logger.info(weibocookies[i].getName() + " : " + weibocookies[i].toString());
}
}
Logger.info("-----------end -------weibocookies-----------------");
Logger.info("---------------sinaCookies-----------------");
Cookie[] sinaCookies = cookiespec.match("sina.com", Contants.LOGON_PORT, "/", false, client.getState().getCookies());
if (weibocookies.length == 0) {
Logger.info("无sinaCookies");
} else {
for (int i = 0; i < sinaCookies.length; i++) {
Logger.info(sinaCookies[i].getName() + " : " + sinaCookies[i].getValue());
}
}
Logger.info("-----------end -------sinaCookies-----------------");
}}