package com.nova.sankuai.infra.utils.baofu.http; import org.apache.commons.lang.StringUtils; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.message.BasicNameValuePair; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * 项目名称:baofoo-fopay-sdk-java * 类名称:表单参数 * 类描述: * 创建人:陈少杰 * 创建时间:2014-10-22 下午2:58:22 * 修改人:陈少杰 * 修改时间:2014-10-22 下午2:58:22 * @version */ public class HttpSendModel implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String url; private String charSet; private HttpMethod method; private List params; private String ContentTypeStr; /** * */ public HttpSendModel() { this.charSet = CharSet.Default; this.method = HttpMethod.getDefault(); this.ContentTypeStr = ""; } /** * @param url */ public HttpSendModel(String url) { super(); this.charSet = CharSet.Default; this.method = HttpMethod.getDefault(); this.ContentTypeStr = ""; int index = url.indexOf("?"); if (index != -1) { this.url = url.substring(0, index); String paramsString = url.substring(index + 1); buildParams(paramsString); } else { this.url = url; } } /** * 注意:
* 比如招行的提交地址https://netpay.cmbchina.com/netpayment/BaseHttp.dll?PrePayC1
* 用get拼好的url串https://netpay.cmbchina.com/netpayment/BaseHttp.dll?PrePayC1? * a=1 这种情况适用
*
* 同时,需要非常注意的是:如果参数中有中文并且你已经url编码过了,那么,请保证url和urlPath保持一致,并使用get方式! * * @param url * @param urlPath * 不带参数的纯地址 * @throws Exception */ public HttpSendModel(String url, String urlPath) throws Exception { super(); charSet = CharSet.Default; method = HttpMethod.getDefault(); if (StringUtils.isBlank(urlPath)) { this.url = url; } else { if (!url.startsWith(urlPath)) { throw new Exception("地址不匹配"); } if (url.length() == urlPath.length()) { this.url = url; return; } if (url.charAt(urlPath.length()) != '?') { throw new Exception("地址不匹配"); } this.url = urlPath; String paramsString = url.substring(urlPath.length() + 1); buildParams(paramsString); } } /** * @param paramsString */ private void buildParams(String paramsString) { if (StringUtils.isBlank(paramsString)) { return; } params = new ArrayList(); String[] keyValue; String[] theParams = paramsString.split("&"); String key; String value; for (String p : theParams) { keyValue = p.split("=", 2); key = keyValue[0]; if (keyValue.length < 2) { value = ""; } else { value = keyValue[1]; } params.add(new HttpFormParameter(key, value)); } } /** * 生成get模式的请求地址 * * @return */ public String buildGetRequestUrl() { String url = this.getUrl(); String charSet = this.getCharSet(); List params = this.getParams(); String requestUrl = url; if (params != null && params.size() != 0) { List qparams = new ArrayList(); for (HttpFormParameter param : params) { qparams.add(new BasicNameValuePair(param.getName(), param .getValue() == null ? "" : param.getValue())); } String appender = URLEncodedUtils.format(qparams, charSet); /** * 20140512 yfj modify * requestUrl += "?" + appender; */ // 用于url不存在参数的情况 if (url.indexOf("?") == -1) { requestUrl += "?" + appender; } // 用于url已存在参数的情况 else { requestUrl += "&" + appender; } } return requestUrl; } /** * @param formName * @return */ public String buildPostRequestForm(String formName) { String url = this.getUrl(); String charSet = this.getCharSet(); List params = this.getParams(); StringBuilder buffer = new StringBuilder(); buffer.append(""); buffer.append("
\n"); for (HttpFormParameter param : params) { if (param.isHidden()) { buffer.append("\n"); } else { buffer.append("\n"); } } buffer.append(""); buffer.append("
"); return buffer.toString(); } /** * @return the url */ public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } /** * @return the charSet */ public String getCharSet() { return charSet; } /** * @param charSet * the charSet to set */ public void setCharSet(String charSet) { this.charSet = charSet; } /** * @return the method */ public HttpMethod getMethod() { return method; } /** * @param method * the method to set */ public void setMethod(HttpMethod method) { this.method = method; } /** * @return the params */ public List getParams() { if (params == null) { params = new ArrayList(); } return this.params; } /** * @param params * the params to set */ public void setParams(List params) { this.params = params; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("[url]" + this.url); if (method != null) { sb.append("[method]" + this.method); } if (params != null && params.size() > 0) { sb.append("[params]-"); for (HttpFormParameter param : params) { sb.append("[" + param.getName() + "]" + param.getValue()); } } return sb.toString(); } public String getContentTypeStr() { return ContentTypeStr; } public void setContentTypeStr(String contentTypeStr) { ContentTypeStr = contentTypeStr; } public static long getSerialversionuid() { return serialVersionUID; } }