 悬赏园豆:50
                [已解决问题] 
            
                    解决于 2021-03-17 09:48
                悬赏园豆:50
                [已解决问题] 
            
                    解决于 2021-03-17 09:48 
                 
        先有一个WebService地址,目前我可以通过SoapUI测试成功并返回数据,但是用POSTMAN和Java的代码拼接同样的请求信息则只能返回WSDL文档内容,不返回数据,请问各位大佬这是什么原因导致的?万分感谢
贴出你的代码看看吧。理论上不应该出现这样的情况
下面是soap请求,
POST /service.soap HTTP/1.1 Host: 192.168.1.2 Content-Type: text/xml; charset=utf-8 Content-Length: 152 SOAPAction: "" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> ******************* </soap:Body> </soap:Envelope>
下面是普通http请求
POST /q HTTP/1.1 Content-Type: application/x-www-form-urlencoded; charset=utf-8 Host: 192.168.1.2 Connection: close User-Agent: Mozila 5.0 Content-Length: 62 k=FOgc3OgR9s2Rl4XfCFL6BNrXqgDCzbP9qkSMweKU5ohkqCg=&v=H7O58qBc7A+8QJE7rqG8duDb7aKo==
你可以发现,除了数据格式不同,其他基本都是一样的。如果返回数据不对,肯定是你写的有问题,抓包看一下吧
public class xxx{
public static void doPostOrGet(String path,String data) {
	PrintWriter out = null;
	String result = "";
	BufferedReader br = null;
	try {
		URL url = new URL(path);
		
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		
		String xmlInfo = getXmlInfo();
		
		byte[] xmlData = xmlInfo.getBytes();
		
		conn.setRequestMethod("POST");
		conn.setUseCaches(false);
		conn.setRequestProperty("accept", "*/*");
		conn.setRequestProperty("Charset", "utf-8");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "text/xml");
        conn.setRequestProperty("Content-length",String.valueOf(xmlData.length));
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
        conn.setRequestProperty("user-agent", "Apache-HttpClient/4.1.1"); 
        conn.setRequestProperty("SOAPAction","XXX");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        String encodings = Base64.getEncoder().encodeToString(("XXX:PPP").getBytes("utf-8"));
       
        conn.setRequestProperty("Authorization", "Basic " + encodings);
        
        conn.connect();
        //out = new PrintWriter(conn.getOutputStream());
        OutputStreamWriter outer = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
        //out.print(xmlData);
       
        //out.flush();
        outer.write(xmlInfo);
        outer.flush();
        outer.close();
        
        int a = conn.getResponseCode();
        String c = conn.getResponseMessage();
        
        BufferedReader br1 = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
        StringBuffer xml = new StringBuffer();
        String str = "";
        while ((str = br1.readLine()) != null) {
        	xml.append(str);
        	System.out.println(str);
        }
        
        br1.close();
        conn.disconnect();	
	}catch(Exception e) {
		e.printStackTrace();
	}finally {
		try {
            if (out != null){
                out.close();
            }
//                if (outer != null){
//                    outer.close();
//                }
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String arg[]) {
	doPostOrGet("XXX","");
}
//拼接请求报文XML
private static String getXmlInfo() {
	StringBuffer sb = new StringBuffer();
		sb.append(" <soapenv:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:urn=\"XXX\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\"> ");
		sb.append(" <soapenv:Header/>");
		sb.append(" <soapenv:Body>");
		sb.append(" <urn:XXX>");
		sb.append(" </urn:XXX>");
		sb.append(" </soapenv:Body>");
		sb.append(" </soapenv:Envelope>");
	
	return sb.toString();
}
}
@你的胖子: 抓包看下就清楚了。看下两个客户端发出的数据有什么不同。再做针对性修改就行
@龙葛格: 按照抓包工具的请求头修改了 但是还是没效果
@龙葛格: 谢了 后来找到了FullPath,操作了一下调试通了~
简单点,找个webservice的java框架调用好了,比如CXF
CXF也试了,没效果