gpt4 book ai didi

java - 尝试将 XML 发布到 HTTPS URL 时出现 HTTP 403 服务错误

转载 作者:行者123 更新时间:2023-12-01 05:37:39 25 4
gpt4 key购买 nike

我正在尝试使用 Apache HttpClient 库编写一个小类,该类将向指定的 URL 发送 HTTPS 请求,发送一些 XML。当我运行代码时,我收到的 HTTP 状态行是“403 服务错误”。以下是返回的完整错误 HTML:

$errorDump  java.net.SocketTimeoutException:Read timed out
$errorInfo
$errorDump java.net.SocketTimeoutException:Read timed out
$error Read timed out
$localizedError Read timed out
$errorType java.net.SocketTimeoutException
$user
$time 2011-10-25 09:39:29 EDT
$error Read timed out
$errorType java.net.SocketTimeoutException

这是我正在使用的代码:

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpXmlPost {

public static void main(String[] args)
{
String url = "https://someurlhere.com";
String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xmlTag></xmlTag>";

String content = request(xmlStr, url);
System.out.println(content);
}

private static String request(String xmlStr, String url) {
boolean success = false;
String content = "";

HttpClient httpclient = new DefaultHttpClient();

try {
HttpPost httpPost = new HttpPost(url.trim());

InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(xmlStr.getBytes() ), -1);
reqEntity.setContentType("application/xml");
reqEntity.setChunked(true);

httpPost.setEntity(reqEntity);

System.out.println("Executing request " + httpPost.getRequestLine());
HttpResponse response = httpclient.execute(httpPost);
HttpEntity resEntity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if(response.getStatusLine().getStatusCode() == 200){
success = true;
}
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
System.out.println("Chunked?: " + resEntity.isChunked());
}

BufferedReader reader = new BufferedReader(new InputStreamReader(resEntity.getContent()));
StringBuilder buf = new StringBuilder();
char[] cbuf = new char[ 2048 ];
int num;

while ( -1 != (num=reader.read( cbuf ))) {
buf.append( cbuf, 0, num );
}

content = buf.toString();

EntityUtils.consume(resEntity);
}
catch (Exception e) {
System.out.println(e);
}
finally {
httpclient.getConnectionManager().shutdown();
}

return content;
}
}

无论我传入什么 XML 似乎都不重要,无论如何它都会给出相同的错误。请注意,这实际上适用于某些 URL。例如,如果我输入 https://www.facebook.com ,它通过了。但是,它不适用于我指定的 URL。我认为这可能是一个证书问题,尝试添加一些代码来信任任何证书,似乎也不起作用,尽管我可能做错了。如有任何帮助,我们将不胜感激。

最佳答案

基于SocketTimeoutException在响应 HTML 的第一行中,我猜测实现您要发布的 URL 的处理程序的组件与生成响应数据所需的源系统存在一些连接问题。

基本上,问题似乎出在服务器上,而不是您的客户端上。

关于java - 尝试将 XML 发布到 HTTPS URL 时出现 HTTP 403 服务错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7890859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com