httpclient.execute(httpPost); 报错 org.apache.http.NoHttpResponseException: The target server failed to respond

利_devil 发布于 08/13 21:10
阅读 92
收藏 0

代理机器地址:10.23.21.1:8080 

应用地址:10.23.1.2:8019

private String httpSend(String url,String xml) throws Exception {
   log.info("开始请求" + url);
   HttpPost httpPost = new HttpPost(url); // 建立连接,需修改IP地址
   //设置客户超时
   RequestConfig defaultRequestConfig = RequestConfig.custom()
         .setSocketTimeout(Integer.valueOf(ConfigVars.getBusiInfoProperty("ConnectTimeout")))
         .setConnectTimeout(Integer.valueOf(ConfigVars.getBusiInfoProperty("SocketTimeout")))
         .setConnectionRequestTimeout(Integer.valueOf(ConfigVars.getBusiInfoProperty("ConnectionRequestTimeout")))
         .setStaleConnectionCheckEnabled(true)
         .build();
   //设置服务端
   RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig).build();
   String proxyState = ConfigVars.getBusiInfoProperty("proxyState");
   //判断是否需要代理
   if (StringUtil.isNotEmpty(ConfigVars.getBusiInfoProperty("proxyState")) && proxyState.equals("true")) {
      HttpHost proxy = new HttpHost(ConfigVars.getBusiInfoProperty("proxyIp"), Integer.parseInt(ConfigVars.getBusiInfoProperty("proxyPort")));
      defaultRequestConfig = RequestConfig.copy(defaultRequestConfig).setProxy(proxy).build();
      requestConfig = RequestConfig.copy(requestConfig).setProxy(proxy).build();
   }
   httpPost.setConfig(requestConfig);
   CloseableHttpClient httpclient = null;
   httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
   CloseableHttpResponse response = null;
   List<NameValuePair> formparams = new ArrayList<NameValuePair>();
   log.info("请求报文:"+xml);
   formparams.add(new BasicNameValuePair("xml",xml));
   UrlEncodedFormEntity uefEntity = null;
   uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
   httpPost.setEntity(uefEntity);
   log.info("开始执行exexcute");
   response = httpclient.execute(httpPost);
   log.info("执行exexcute--完毕");
   HttpEntity entity = response.getEntity();
   log.info("获得entity"+entity);
   String result =  EntityUtils.toString(entity, "UTF-8");
   EntityUtils.consume(entity);
   return result;
}

nginx配置:


user  root;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  102400;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
   
   upstream sss {
        server 101.3.20.116:9080;
    }

    server {
        listen       8080;
        server_name  10.23.21.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

       location / {
            proxy_pass http://sss;
            proxy_set_header Host $host:8080;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_buffer_size   128k;
            proxy_buffers   4 256k;
            proxy_busy_buffers_size   256k;
       }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

}
 

当执行; response = httpclient.execute(httpPost),会报错提示:org.apache.http.NoHttpResponseException: The target server failed to respond:

当执行

httpclient.execute(httpGet)请求会响应成功,但是服务器不允许get请求,必须是post请求

想问一下,这个问题出现的原因是什么??

加载中
0
利_devil

在代理机上,可以直接访问目标地址

0
利_devil

formparams.add(new BasicNameValuePair("xml",xml));
        UrlEncodedFormEntity uefEntity = null;
        uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httpPost.setEntity(uefEntity);

response = httpclient.execute(httpPost);

 

设置参数后,请求就会抛异常,没有参数可以通过。

返回顶部
顶部