ajax访问WebService跨域问题

public static void main(String[] args) throws IOException { /* *1.创建一个url *2.打开一个连接 *3.设置相关参数 *4.创建输出流,用来发送SAOP请求 *5.发送完,接收数据 *6.用输入流获取webservice中的内容 */ URL url = new URL("http://localhost:5555/hello"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST");//必须设置为POST方式,而且必须是大写的 connection.setDoInput(true);//因为有输入参数也有输出参数所以都为真 connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8"); OutputStream out = connection.getOutputStream(); //下面替换尖括号是测试传送xml文本字符串测试的 //String str="<list><item><email>aaa!qq</email></item></list>"; //str=str.replaceAll("<","&lt;").replaceAll(">","&gt;"); StringBuilder soap=new StringBuilder(); soap.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://server.hjp.com/\">"); soap.append("<soapenv:Header/>"); soap.append("<soapenv:Body>"); soap.append("<ser:sayHello>"); soap.append("<arg0>aaa</arg0>"); soap.append("</ser:sayHello>"); soap.append("</soapenv:Body>"); soap.append("</soapenv:Envelope>"); String argo=soap.toString(); System.out.println(argo); out.write(argo.getBytes());//发送SAOP请求 InputStream stream = connection.getInputStream(); byte[] b = new byte[1024]; int len=0; StringBuffer buffer = new StringBuffer(); while((len=stream.read(b))!=-1){ String s = new String(b, 0, len, "utf-8"); buffer.append(s); } System.out.println(buffer.toString()); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/6e1744a84e6abf83016c286611e01f99.html