2010-06-28 82 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; 
} 
相關問題