gpt4 book ai didi

java - string.format 在 http post 请求和响应中抛出异常

转载 作者:行者123 更新时间:2023-11-28 23:38:40 26 4
gpt4 key购买 nike

我想要一种放置格式说明符(如 %s)而不是放置硬代码值的方法,您可以在下面找到我的代码;

String userName = "Nicole";
String password = "Nicole";

HttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost("url");

String input=String.format("<soap:Envelope

xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:inn=\"http:Innovation/\">

<soap:Header/><soap:Body><inn:getCategoriesbyVendorID><!--Optional:-->

<userName>%s</userName><!--Optional:--><password>%s</password></inn:getCategoriesbyVendorID>

</soap:Body></soap:Envelope>",userName,password);

//StringEntity input = new StringEntity("<soap:Envelope

xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:inn=\"http:Innovation/\">

<soap:Header/><soap:Body><inn:getCategoriesbyVendorID><!--Optional:-->

<userName>Nicole</userName><!--Optional:--><password>Nicole</password>

</inn:getCategoriesbyVendorID></soap:Body></soap:Envelope>");

post.setEntity(input);

HttpResponse response = client.execute(post);

BufferedReader rd = new BufferedReader(new

InputStreamReader(response.getEntity().getContent()));

String line = "";

while ((line = rd.readLine()) != null) {

System.out.println(line);

使用上面的代码,实体抛出一个错误。谁能帮我解决这个问题?

最佳答案

您的字符串格式化语法没有任何问题。

It is showing an error in eclipse i e.,change type of input to HTTP entity(post. set Entity(input);).

错误是因为HttpPost.setEntity需要一个类型为 HttpEntity 的参数当您为其指定 String 的类型时。

尝试设置一个 org.apache.http.entity.StringEntity相反:

HttpEntity entity = new StringEntity(input); //Your formatted String 
post.setEntity(entity);

关于java - string.format 在 http post 请求和响应中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22218807/

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