Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HTTPPost在执行方法中的使用错误_Java_Https - Fatal编程技术网

Java HTTPPost在执行方法中的使用错误

Java HTTPPost在执行方法中的使用错误,java,https,Java,Https,我正在使用下面的代码来创建一个http客户端,我在执行方法中遇到了问题,它被卡住了 public static final HttpHost target = new HttpHost("test.xyz.com", 443, "https"); public static void test() { HttpEntity responseEntity = null; DefaultHttpClient client = new DefaultHttpClient();

我正在使用下面的代码来创建一个http客户端,我在执行方法中遇到了问题,它被卡住了

public static final HttpHost target = new HttpHost("test.xyz.com", 443, "https");

public static void test()
{
    HttpEntity responseEntity = null;
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("/xyz/test");
    System.out.println("post is " +post.getRequestLine());

        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        AuthScope authScope = new AuthScope(target.getHostName(), target.getPort());
        System.out.println("auth scope is " +authScope);
        client.getCredentialsProvider().setCredentials(authScope, credentials);
        //i am passing a xml here
        post.setEntity(new StringEntity(xml, "UTF-8"));
        post.addHeader("Content-Type", "application/xml");
        post.addHeader("Accept", "application/xml");
        System.out.println("post " +post.getAllHeaders().length);
        HttpResponse response = client.execute(target, post); //getting stuck here no response at all
        System.out.println("response  " +response.getStatusLine());
        responseEntity = response.getEntity();
        System.out.println("response entity " +responseEntity);
        String responseXmlString = EntityUtils.toString(responseEntity);
        System.out.println("response" +responseXmlString);


}
我在这里面临问题

HttpResponse response = client.execute(target, post); 
可能有什么问题


问题是代码在client.execut方法中被卡住了。它没有进一步移动,我也没有收到任何响应。我必须设置任何代理吗?

可能您的请求没有真正被卡住,只是在等待TCP连接超时,默认情况下可能相当长—大约5分钟。应该有一种方法可以将超时设置为更短的值—查看http客户端的javadoc,或者尝试等待更长的时间


此外,您还可以在程序被阻塞时进行线程转储,以查看线程被阻塞的位置(请参阅Sun JDK中的jps和jstack实用程序)

我认为您首先需要消除服务器行为异常的可能性。您是否尝试过从程序外部发布到该URL。如果您使用的是类unix系统,或者使用的是windows,并且有cygwin,请尝试以下操作:

cat <yourfile.xml> | curl -X POST -H 'Content-type: text/xml' -d @- https://test.xyz.com:443/xyz/test
cat|curl-X POST-H'内容类型:text/xml'-d@-https://test.xyz.com:443/xyz/test

如果POST成功,请获取程序的线程转储,并将其发布到此处供我们查看。

问题在于代码卡在client.execut方法中。它没有进一步移动。我也没有收到任何响应。我是否需要设置任何代理?运行测试时,您是否有可以接收POST的目标设置?execute可能正忙于将数据发送到它无法写入的套接字。ya目标设置为接收数据。服务器可能忙于接收数据的时间有多长,我等待它执行atmost 5分钟服务器访问日志是否显示连接已建立?servlet/jsp是否接收到任何数据?这工作正常,通过curl我可以访问服务器,通过java程序我被绊倒了,而没有得到任何信息