如何在java中调用c#的web服务?我创建了一个调用C#Web服务的Java应用程序。这是代码。包调用webserviceadd;导入java.util.Iterator;导入javax.xml.soap.*;导入javax.xml.transform.*;导入javax.xml.transform.stream.StreamResult;导入org.w3c.dom.Node;publicclassCallWebServiceAdd{publicstaticvoidmain(String[]args){//TODO代码应用逻辑在这里Stringa="10";字符串b="20";Stringop="Addweb";Stringurn="WebService1";Stringdest="http://localhost:1267/WebService1.asmx";尝试{SOAPConnectionFactorysoapConnFact=SOAPConnectionFactory.newInstance();SOAPConnectionconn=soapConnFact.createConnection();MessageFactorymsgFact=MessageFactory.newInstance();SOAPMessage消息=msgFact.createMessage();SOAPPartsoapPart=msg.getSOAPPart();SOAPEnvelope信封=soapPart.getEnvelope();envelop.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");SOAPBodybody=envelop.getBody();QNamebodyName=newQName("http://localhost:1267/","Addweb");SOAPElementbodyElement=body.add主体元素(主体名称);QName名称=newQName("A");SOAPElement符号=bodyElement.addChildElement(名称);symbol.addTextNode(a);/*SOAPElementbodyelement=body.addChildElement("Addition");SOAPElementbodyelement1=bodyelement.addChildElement("a").addTextNode(a);SOAPElementbodyelement2=bodyelement.addChildElement("b").addTextNode(b);*/MimeHeadersheaders=msg.getMimeHeaders();headers.addHeader("SOAPAction","http://localhost:1267/WebService1.asmx");//msg.writeTo(System.out);msg.saveChanges();SOAPMessagereply=conn.call(msg,dest);soapPart=reply.getSOAPPart();envelop=soapPart.getEnvelope();body=envelop.getBody();迭代器iter=body.getChildElements();节点resultOuter=((Node)iter.next()).getFirstChild();节点结果=resultOuter.getFirstChild();System.out.println("add("+a+","+b+")="+result.getNodeValue());reply.writeTo(System.out);conn.close();}catch(Exceptione){System.out.println(e.getMessage());}}}错误服务器无法识别HTTP标头SOAPAction的值:http://localhost:1267/WebService1.asmx预期结果add(10,20)=30c#webservice代码publicclassWebService1:System.Web.Services。WebService{[WebMethod]publicintAddProg(inta,intb){returna+b;}}返回XMLsoap:ClientSystem.Web.Services.Protocols.SoapException:服务器无法识别HTTP标头SOAPAction的值:http://localhost:1267/WebService1.asmx。在System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()在System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage消息).Protocols.ServerProtocol.SetContext(类型类型,HttpContext上下文,HttpRequest请求,HttpResponse响应)在System.Web.Services.Protocols.ServerProtocolFactory.Create(类型类型,HttpContext上下文,HttpRequest请求,HttpResponse响应,Boolean&abortProcessing)BUILDSUCCESSFUL(总时间:1秒)标头有什么问题?编辑您在java代码中指定的WebService1.wsdlsoapActionheaders.addHeader("SOAPAction","http://localhost:1267/WebService1.asmx");headers.addHeader("SOAPAction","http://localhost:1267/WebService1.asmx");应该对应于您所针对的操作指定的soapAction,即http://tempuri.org/HelloWorld或http://tempuri.org/AddwebSOAPAction标头的值是错误的。对于每个操作,应该在WSDL中给出正确的值。例如,http://tempuri.org/HelloWorld用于HelloWorld操作或http://tempuri.org/Addweb用于Addweb操作。以上就是C#学习教程:如何在java中调用c#的web服务?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
