HttpPost请求不成功

时间:2014-06-05 18:28:02

标签: java servlets apache-httpclient-4.x

我已经编写了一个Web服务,现在正在编写一个测试人员来从外部执行集成测试。我正在使用apache httpclient 4.3编写我的测试人员。根据此处的代码:http://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html和此处:http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/fundamentals.html#d5e186我编写了以下代码。

    Map<String, String> parms = new HashMap<>();
    parms.put(AirController.KEY_VALUE, json);
    postUrl(SERVLET, parms);
    ...

protected String postUrl(String servletName, Map<String, String> parms)
        throws AirException{
    String url = rootUrl + servletName;
    HttpPost post = new HttpPost(url);

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    for(Map.Entry<String, String> entry:parms.entrySet()){
        BasicNameValuePair parm = new BasicNameValuePair(entry.getKey(), entry.getValue());
        nvps.add(parm);
    }

    try {
        post.setEntity(new UrlEncodedFormEntity(nvps));
    } catch (UnsupportedEncodingException use) {
        String msg = "Invalid parameters:" + parms;
        throw new AirException(msg, use);
    }

    CloseableHttpResponse response;
    try {
        response = httpclient.execute(post);
    } catch (IOException ioe) {
        throw new AirException(ioe);
    }

    String result;
    if(HttpStatus.SC_OK == response.getStatusLine().getStatusCode()){
        result = processResponse(response);
    }
    else{
        String msg = MessageFormat.format("Invalid status code {0} received from query {1}.",
                                            new Object[]{response.getStatusLine().getStatusCode(), url});
        throw new AirException(msg);
    }

    return result;
}

此代码成功到达我的servlet。在我的servlet中,我(使用Spring的AbstractController):

protected ModelAndView post(HttpServletRequest request, HttpServletResponse response) {
    String json = String.valueOf(request.getParameter(KEY_VALUE));
    if(json.equals("null")){
        log.info("Received null value.");
        response.setStatus(406);
        return null;
    }

此代码始终属于null参数代码并返回406。

我确定我错过了一些简单的东西,但看不出它是什么。

1 个答案:

答案 0 :(得分:0)

在没有转储标题的情况下,没有看到如何进行调试?

  post.setEntity(new UrlEncodedFormEntity(nvps));

你不必知道上面的标题是什么? 你不必确切地知道你的代码正在编组的标题集合吗?

您可以首先使用curl / wget作为服务的客户端进行测试,使用详细信息确保您在服务的成功帖子中使用的标题集。

然后,只需使用您的Java客户端接近那组头文件就可以了。

例如,您的Java客户端需要使用RequestInterceptor实现设置哪些东西?

CloseableHttpClient httpClient = HttpClients.custom()     .setConnectionManager(YourConnectionMgr.getInstance())
.addInterceptorLast(new HttpRequestInterceptor() {
    public void process(
    final HttpRequest request, 
final HttpContext context) throws HttpException, IOException { 
..
                                        request.addHeader("Content-Type", "application/json") ;
                                        request.addHeader("Content-Type", mimeType) ;
                                    request.removeHeaders("Accept-Encoding");
                                    request.removeHeaders("Content-Type");
                                    request.addHeader("Expect", "100-continue");
                                    request.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") ;

要获得正确的标题可能会解决您的服务器端问题,但您需要公开标题,甚至可能会暴露WIRE级记录器。

相关问题