首页 新闻 会员 周边 捐助

关于WebService的问题

0
悬赏园豆:50 [已解决问题] 解决于 2021-03-17 09:48

先有一个WebService地址,目前我可以通过SoapUI测试成功并返回数据,但是用POSTMAN和Java的代码拼接同样的请求信息则只能返回WSDL文档内容,不返回数据,请问各位大佬这是什么原因导致的?万分感谢

你的胖子的主页 你的胖子 | 初学一级 | 园豆:11
提问于:2021-03-12 15:12
< >
分享
最佳答案
0

贴出你的代码看看吧。理论上不应该出现这样的情况

下面是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==

你可以发现,除了数据格式不同,其他基本都是一样的。如果返回数据不对,肯定是你写的有问题,抓包看一下吧

收获园豆:50
龙葛格 | 小虾三级 |园豆:782 | 2021-03-12 16:35

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();
}

}

你的胖子 | 园豆:11 (初学一级) | 2021-03-12 18:31

@你的胖子: 抓包看下就清楚了。看下两个客户端发出的数据有什么不同。再做针对性修改就行

龙葛格 | 园豆:782 (小虾三级) | 2021-03-15 13:32

@龙葛格: 按照抓包工具的请求头修改了 但是还是没效果

你的胖子 | 园豆:11 (初学一级) | 2021-03-16 12:28

@龙葛格: 谢了 后来找到了FullPath,操作了一下调试通了~

你的胖子 | 园豆:11 (初学一级) | 2021-03-17 09:49
其他回答(1)
0

简单点,找个webservice的java框架调用好了,比如CXF

8号位 | 园豆:596 (小虾三级) | 2021-03-16 10:07

CXF也试了,没效果

支持(0) 反对(0) 你的胖子 | 园豆:11 (初学一级) | 2021-03-16 12:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册