java 调用webservice 求解

空_白blue 2014-10-08 08:48:12
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.Header;
import org.apache.soap.SOAPException;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
import org.apache.soap.util.xml.QName;


public class TestWebService {
public static String getService() {
//http://appkey.imjuju.com:8200/api/chatmanage.asmx
URL url = null;
try {
url=new URL("http://appkey.imjuju.com:8200/api/chatmanage.asmx");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-UserLogin");
// This is the name of the method on the above object
soapCall.setMethodName("UserLogin");
Header header = new Header();
header.setAttribute(new QName("http://tempuri.org/", "UserLogin"), "");
soapCall.setHeader(header);
//soapCall.setOperationName(new QName("", "GetTestQuestions"));
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
// Parameter isbnParam = new Parameter("userName", String.class, user, null);
Parameter isbnParam1 = new Parameter("loginname", String.class, "no1", null);
Parameter isbnParam2 = new Parameter("loginpwd", String.class, "123456", null);
Parameter isbnParam3 = new Parameter("lat", Float.class, 1.2, null);
Parameter isbnParam4 = new Parameter("lng", Float.class, 1.20, null);
soapParams.addElement(isbnParam1);
soapParams.addElement(isbnParam2);
soapParams.addElement(isbnParam3);
soapParams.addElement(isbnParam4);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {

return se.getMessage();

}

}
public static void main(String[] args) {
System.out.println(getService());
}
}
报错:
System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: .
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
...全文
365 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
空_白blue 2014-10-18
  • 打赏
  • 举报
回复
引用 8 楼 ix_fly 的回复:
不知道你解决了没,为了方便用的xfire,jar官方下载即可:

@Test
	public void testUserLogin(){
		try {
			String methodName = "UserLogin";
			String userName = "no1";
			String passWord = "123456";
			float lat = 1.2f;
			float lng = 1.20f;
			String url = "http://appkey.imjuju.com:8200/api/chatmanage.asmx?wsdl";
			Client client = new Client(new URL(url));
			
			Object[] resultInfo = client.invoke(methodName,new Object[]{userName,passWord,lat,lng});
			if (null != resultInfo && resultInfo.length > 0) {
				System.out.println(resultInfo[0]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
非常感谢,已经解决
ix_fly 2014-10-08
  • 打赏
  • 举报
回复
不知道你解决了没,为了方便用的xfire,jar官方下载即可:

@Test
public void testUserLogin(){
try {
String methodName = "UserLogin";
String userName = "no1";
String passWord = "123456";
float lat = 1.2f;
float lng = 1.20f;
String url = "http://appkey.imjuju.com:8200/api/chatmanage.asmx?wsdl";
Client client = new Client(new URL(url));

Object[] resultInfo = client.invoke(methodName,new Object[]{userName,passWord,lat,lng});
if (null != resultInfo && resultInfo.length > 0) {
System.out.println(resultInfo[0]);
}
} catch (Exception e) {
e.printStackTrace();
}
}


空_白blue 2014-10-08
  • 打赏
  • 举报
回复
引用 3 楼 fangmingshijie 的回复:
调用第三方webservice api要严格按照别人提供的调用方式。这个错误信息告诉你,无法识别你的soapaction,那你就不要设置soapactionname了,改为soapaction。
import java.util.Set; import java.util.Vector; import org.apache.soap.Constants; import org.apache.soap.Fault; import org.apache.soap.SOAPException; import org.apache.soap.rpc.Call; import org.apache.soap.rpc.Parameter; import org.apache.soap.rpc.Response; public class WebServiceUtil { public static String getService(String requesturl,Map parameters) { URL url = null; try { url=new URL(requesturl); } catch (MalformedURLException mue) { return mue.getMessage(); } // This is the main SOAP object Call soapCall = new Call(); // Use SOAP encoding //http://appkey.imjuju.com:8200/api/chatmanage.asmx/UserLogin //?loginname=no1&loginpwd=123456&lat=1213&lng=79 //soapCall.setEncodingStyleURI("http://schemas.xmlsoap.org/soap/encoding/"); soapCall.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC); // This is the remote object we're asking for the price soapCall.setTargetObjectURI("urn:xmethods-ChatManage"); // This is the name of the method on the above object soapCall.setMethodName("UserLogin"); // We need to send the ISBN number as an input parameter to the method Vector soapParams = new Vector(); // name, type, value, encoding style if(parameters!=null){ Set<String> set = parameters.keySet(); Parameter isbnParam = null; for(String key:set){ isbnParam = new Parameter(key, String.class, parameters.get(key),null); soapParams.addElement(isbnParam); } } soapCall.setParams(soapParams); try { // Invoke the remote method on the object Response soapResponse = soapCall.invoke(url,""); // Check to see if there is an error, return "N/A" if (soapResponse.generatedFault()) { Fault fault = soapResponse.getFault(); String f = fault.getFaultString(); return f; } else { // read result Parameter soapResult = soapResponse.getReturnValue (); // get a string from the result return soapResult.getValue().toString(); } } catch (SOAPException se) { return se.getMessage(); } } public static void main(String[] args) { String url ="http://appkey.imjuju.com:8200/api/chatmanage.asmx"; Map<String,String> parameters = new HashMap<String, String>(); parameters.put("loginname", "no1"); parameters.put("loginpwd","123456"); parameters.put("lat","123456"); parameters.put("lng","123456"); String a = getService(url, parameters); System.out.println(a); } } 一样的错误: System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: . at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
空_白blue 2014-10-08
  • 打赏
  • 举报
回复
引用 4 楼 shixitong 的回复:
[quote=引用 2 楼 u010784851 的回复:] 命名空间不是默认的吗
把这个文件贴出来看看[/quote] 文件地址: http://appkey.imjuju.com:8200/api/chatmanage.asmx 访问的 UserLogin
空_白blue 2014-10-08
  • 打赏
  • 举报
回复
文件地址: http://appkey.imjuju.com:8200/api/chatmanage.asmx 访问的 UserLogin
shixitong 2014-10-08
  • 打赏
  • 举报
回复
引用 2 楼 u010784851 的回复:
命名空间不是默认的吗
把这个文件贴出来看看
  • 打赏
  • 举报
回复
调用第三方webservice api要严格按照别人提供的调用方式。这个错误信息告诉你,无法识别你的soapaction,那你就不要设置soapactionname了,改为soapaction。
空_白blue 2014-10-08
  • 打赏
  • 举报
回复
命名空间不是默认的吗
shixitong 2014-10-08
  • 打赏
  • 举报
回复
看看这个文件的chatmanage.asmx命名空间是不是正确

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧