如下代码,我在Java通过HttpClient调用C#发布的Webservice。
C#返回一个JSON格式字符串,现验证JSON格式正确。
返回结果却提示:服务器无法为请求提供服务,因为不支持该媒体类型。
请各位帮忙看看。(代码如下)。
public class Test { public static void main(String[] args) { HttpPost httpPost = new HttpPost(
"http://192.168.123.204/MSAdmin/XXX.asmx?op=GetNewsList");
HttpClient httpClient = new DefaultHttpClient();
try {
ContentProducer cp = new ContentProducer() {
@Override
public void writeTo(OutputStream outstream) throws IOException {
Writer writer = new OutputStreamWriter(outstream, "UTF-8");
String requestXml = getRequestXml();
writer.write(requestXml);
writer.flush();
}
};

httpPost.addHeader("Content-Type", "text;charset=UTF-8");
httpPost.setEntity(new EntityTemplate(cp));
HttpResponse response = httpClient.execute(httpPost);
                        System.out.println(EntityUtils.toString(response.getEntity(),"UTF-8"));
} catch (Exception e) {
System.out.println("异常");
e.printStackTrace();
}
} private static String getRequestXml(){
StringBuilder sb = new StringBuilder("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.append("<s:Header>");
sb.append("<Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/GetNewsList</Action>");
sb.append("</s:Header>");
sb.append("<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
sb.append("<GetNewsList xmlns=\"http://tempuri.org/\" />");
sb.append("</s:Body>");
sb.append("</s:Envelope>");
return sb.toString();
}
}
JavaWebservice

解决方案 »

  1.   

    StringBuilder这个类是线程非安全的。改为StringBuffer看看。错误类型是415?
      

  2.   

     httpPost.addHeader("Content-Type", "text;charset=UTF-8");//这个类型挺稀罕,没见过text的。改为text/html或者application/text之类的。
      

  3.   

    回上一楼,那个类型不对,是少些了一段,不过最后验证的确是这个类型的问题。
    结果为    text/xml;charset=utf-8      
    谢谢各位解答。