C#调用java写的Webservice

 时间:2024-09-21 00:33:09

万能调用webservice由于webservice是基于http和xml的,所以通过httprequst发送xml这种方式对于任何语言写的Webservice都适用

方法/步骤

1、webservice调用帮助方法基于xml+丝群闻栝http///<summary>///We芟鲠阻缒bService调用帮助类(基于xml+http请求)///add:jzliu///date:2018-03-13///</summary>publicclassWebServiceHelper{publicstaticTPostWebService<T>(stringuri,stringrequestSoapXml){try{//请求的soapxml字节序列byte[]bytData=Encoding.UTF8.GetBytes(requestSoapXml);//创建一个HttpWebRequest实例HttpWebRequestrequest=System.Net.WebRequest.Create(newUri(uri))asHttpWebRequest;//设置代理(如果需要代理访问)stringproxyIP=WebConfigCommon.ProxyHostIP;stringproxyPort=WebConfigCommon.ProxyHostPort;//如果配置了代理ip则使用代理ip否则不使用if(!string.IsNullOrEmpty(proxyIP)&&!string.IsNullOrEmpty(proxyPort)){WebProxyproxy=newWebProxy(proxyIP,Convert.ToInt32(proxyPort));request.Proxy=proxy;}//按照SOAP结构中描述的给各个属性赋值request.Method="POST";//POST方式传输request.ContentType="text/xml;charset=utf-8";//传输内容类型及编码格式request.ContentLength=bytData.Length;//传输内容长度//默认身份认证request.Credentials=CredentialCache.DefaultCredentials;//设置超时时间为10分钟request.Timeout=600000;//用GetRequestStream()方法来获取一个流,它发出的请求将数据发送到Internet资源供给接口StreamnewStream=request.GetRequestStream();//将数据写入该流newStream.Write(bytData,0,bytData.Length);//获取一个响应HttpWebResponseres=(HttpWebResponse)request.GetResponse();//将响应写入StreamReaderStreamReadersr=newStreamReader(res.GetResponseStream(),Encoding.UTF8);//读取流转换成字符串stringret=sr.ReadToEnd();stringretttt=ret.ToString();//释放相关非托管资源sr.Close();res.Close();newStream.Close();ret=WebServiceResponseSoapHelper.GetResponseResultStrings(ret);returnXmlHelper.Deserialize<T>(ret);}catch(Exceptionex)//抛出异常由顶层调用接收并处理{throwex;}}}

2、webservice调用帮助方法_方法参数之_requestSoapXmlstringrequestSoapXml="<?xmlversion=\"1.0\"encoding=\"utf-8\"?>"+"<soap:Envelopexmlns: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>"+"<HelloWorldxmlns=\"http://tempuri.org/\">"+"<inputname>"+"你好世界"+"</inputname>"+"</HelloWorld>"+"</soap:Body>"+"</soap:Envelope>";

3、webservice调用帮助方法_方法参数之uri服务即wsdl地址

在EPLAN里面设计交流接触器检验电路的主触点 步科触摸屏显示元件怎么使用? inventor安装失败怎样卸载inventor?错误提示 win10系统怎么诊断网络? 如何自己设置软件的快捷键
热门搜索
蚂蚱图片 日本邪恶动漫 馒头图片 黄杨树图片 少女邪恶漫画无翼鸟