2010-06-28 110 views
0

我已经创建了一个构造url来发送POST请求的java类。我必须使用content-type(application/x-www-form-urlencoded)而不使用HttpServletResponse /来自简单的java类的请求。我应该怎么做?如何在没有HttpServlet的情况下为post请求设置内容类型

+0

您使用的特别或HttpURLConnection的任何框架? (这听起来像你在谈论客户端的使用,即使你提到HttpServletResponse/Request。) – Bruno 2010-06-28 14:53:17

+0

我正在使用HttpClient。 – 2010-06-29 07:37:51

回答

0

下面是用于发布使用Apache HTTPComponents 4.2

这是处理用于发布数据,不仅形式-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); 
    } 
} 

为了饼干传送到请求:

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编码:

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); 
} 

要trans FER POST数据,但作为原始数据(没有形成 - URL编码):

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; 
} 
相关问题