我创建了一个构造url来发送POST请求的java类。我必须使用内容类型(application / x-www-form-urlencoded)而不使用简单java类的HttpServletResponse / Request。我应该怎么做?
答案 0 :(得分:0)
以下是使用apache HTTPComponents 4.2
发布的一些代码这是一种骨架方法,可以处理POST数据的每种可能情况,而不仅仅是form-url编码的情况。
public void doPost(String destinationUrl,String contentType,
final Map<String,String> headers,final Cookie[] cookies,
final String postData) throws IOException,
ServletException {
// [1] Create the POST request
ContentType contentType = ContentType.create(contentType);
log.debug("POST Request URL: {} - Content-Type: {}",
destinationUrl,
contentType);
HttpPost postRequest = new HttpPost(destinationUrl);
// [2] Transfer headers/cookies
_transferRequestHeaders(headers,
postRequest);
_transferRequestCookies(cookies,
postRequest);
// [3] post data
if (contentType == null ||
ContentType.APPLICATION_FORM_URLENCODED.equals(contentType)) {
_transferFormUrlEncodedPost(postdata,
postRequest);
} else {
_transferContentPost(postData,contentType
postRequestToBeProxied);
}
// [4] Execute the proxy request
_doPost(postRequest);
}
为了传输请求标头:
private void _transferRequestHeaders(final Map<String,String> headers,
final HttpRequestBase postRequest) {
for (Map.Entry<String,String> me : headers.entrySet()) {
Header header = new BasicHeader(me.getKey(),
me.getValue());
postRequest.setHeader(header);
}
}
为了将cookie转移到请求中:
private void _transferRequestCookies(final Cookie[] cookies,
final HttpRequestBase request) {
if (cookies == null) return;
String cookiesStr = "";
for (Cookie cookie : cookies) {
cookie.setDomain(domain);
cookie.setPath(path);
cookiesStr = cookiesStr + " " + cookie.getName() + "=" + cookie.getValue() + "; Path=" + cookie.getPath() + ";";
}
request.setHeader("Cookie", cookiesStr);
}
要传输POST数据表单 - url encoded:
private void _transferFormUrlEncodedPost(Map<String,String[]> postParams,
final HttpPost postRequest) throws UnsupportedEncodingException {
// Create a List to hold the NameValuePairs to be passed to the PostMethod
List<NameValuePair> nameAndValuePairs = new ArrayList<NameValuePair>();
for (String paramName : postParams.keySet()) {
// Iterate the values for each parameter name
String[] paramValues = postParams.get(paramName);
for (String paramValue : paramValues) {
NameValuePair nameValuePair = new BasicNameValuePair(paramName,paramValue);
nameAndValuePairs.add(nameValuePair);
}
}
// Set the request POST data
UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity(nameAndValuePairs);
postRequest.setEntity(paramEntity);
}
要传输POST数据,但作为原始数据(NOT form - url encoded):
private void _transferContentPost(final String postContent,ContentType contentType,
final HttpPost postRequest) throws IOException,
ServletException {
// [3] Hand de POST data
StringEntity entity = new StringEntity(postContent,
contentType);
postRequest.setEntity(entity);
}
最后做POST:
private HttpServletResponse _doPost(final HttpRequestBase postRequest) throws
IOException,
ServletException {
// [1] - Create a default HttpClient
HttpParams httpClientParams = new BasicHttpParams();
HttpClientParams.setRedirecting(httpClientParams,false);
httpClientParams.setParameter(ClientPNames.HANDLE_REDIRECTS,false);
httpClientParams.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS,false);
HttpClient httpClient = new SystemDefaultHttpClient(httpClientParams);
// [2] - Execute the request
HttpResponse endPointResponse = httpClient.execute(postRequest);
return endPointResponse;
}