gpt4 book ai didi

java - Apache HTTP 客户端 400 错误

转载 作者:行者123 更新时间:2023-12-02 11:05:20 24 4
gpt4 key购买 nike

我试图自动化 Web 服务,并以字符串形式传递 XML,然后将其转换为字符串实体并设置实体。但我不知道为什么它会抛出 400 错误。我是 WebServices Automation 的新手,请在这方面帮助我。

下面是我的代码:

package com.WebServices.Automation;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.junit.Test;

public class HTTPClientA {

static String url = "http://www.dneonline.com/calculator.asmx?wsdl";

String xml = "\"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">\r\n" +
" <soap:Header/>\r\n" +
" <soap:Body>\r\n" +
" <tem:Add>\r\n" +
" <tem:intA>10</tem:intA>\r\n" +
" <tem:intB>20</tem:intB>\r\n" +
" </tem:Add>\r\n" +
" </soap:Body>\r\n" +
"</soap:Envelope>";
@Test
public void main() throws ClientProtocolException, IOException
{

StringEntity stringEntity = new StringEntity(xml);
HttpPost post = new HttpPost(url);
post.setEntity(stringEntity);
HttpClient client = HttpClientBuilder.create().build();
post.setHeader("Content-Type", "text/xml; charset=utf-8");
post.setHeader("SOAPAction", "http://tempuri.org/Add");
HttpResponse res = client.execute(post);

int actualresponse = res.getStatusLine().getStatusCode();
System.out.println(actualresponse);
try
{
Assert.assertEquals(actualresponse, 200);
}
catch (Exception e) {
// TODO: handle exception
}
HttpEntity entity = res.getEntity();

String strResponse = null;
if (entity != null) {
strResponse = EntityUtils.toString(entity);
System.out.println(strResponse);
}
}

}

最佳答案

您的 XML 无效,它以双引号开头,而不是 <?xml ,将您的作业更改为:

  String xml = "<?xml version ... –

关于java - Apache HTTP 客户端 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51010952/

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