我正在尝试通过HTTP调用SOAP API,我需要一些建议。问题是,当我将属性设置为说内容类型应为“ text / xml”时,没有设置它:
URL url = new URL(SOAP_URI);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Accept","text/xml charset=utf-8");
System.out.println(con.getContentType());
当我打印出con.getContentType时,它也会打印出来:
text/html; charset=UTF-8
如何将其设置为text / xml charset = utf-8?
答案 0 :(得分:1)
con.getContentType
返回 URL资源的 Content-Type 标头字段的值,而setRequestProperty("Accept")
更改的属性接受请求的标头。
尝试getRequestProperty
来获取请求的 Accept 标头:
System.out.println(con.getRequestProperty("Accept"));
要设置和打印请求的Content-Type
标头:
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
System.out.println(con.getRequestProperty("Content-Type"));