webservice客户端用HttpURLConnection实现与注意事项
webservice客户端用HttpURLConnection实现与注意事项
URL url = new URL(""); URLConnection conn; conn = url.openConnection(); HttpURLConnection con =(HttpURLConnection)conn; con.setDefaultUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("content-type", "text/xml;charset=GBK"); //需要加SOAPAction,不然可能会报错 con.setRequestProperty("SOAPAction", ""); OutputStream io=con.getOutputStream(); String xml=""; io.write(xml.getBytes()); io.flush(); io.close(); System.out.println(con.getResponseCode()); //可以通过这个查看报错信息:con.getErrorStream() InputStream is=con.getInputStream(); InputStreamReader isr=new InputStreamReader(is, "UTF-8"); BufferedReader br =new BufferedReader(isr); StringBuilder b=new StringBuilder(); String temp=null; while (null!=(temp=br.readLine())) { b.append(temp); } //返回的中文信息会受网页格式影响,需要解码 System.out.println(StringEscapeUtils.unescapeHtml(b.toString()));
                    
                
                
            
        
浙公网安备 33010602011771号