C#调用java写的Webservice

2025-09-27 03:12:04

1、webservice 调用帮助方法基于 xml+http

    /// <summary>

    /// WebService调用帮助类(基于xml+http请求)

    /// add: jzliu

    /// date: 2018-03-13

    /// </summary>

    public class WebServiceHelper

    {

        public static T PostWebService<T>(string uri, string requestSoapXml)

        {

            try

            {

                

                //请求的soapxml字节序列

                byte[] bytData = Encoding.UTF8.GetBytes(requestSoapXml);

                //创建一个HttpWebRequest实例

                HttpWebRequest request = System.Net.WebRequest.Create(new Uri(uri)) as HttpWebRequest;

                //设置代理(如果需要代理访问)

                string proxyIP = WebConfigCommon.ProxyHostIP;

                string proxyPort = WebConfigCommon.ProxyHostPort;

                //如果配置了代理ip则使用代理ip否则不使用

                if (!string.IsNullOrEmpty(proxyIP) && !string.IsNullOrEmpty(proxyPort))

                {

                    WebProxy proxy = new WebProxy(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资源供给接口  

                Stream newStream = request.GetRequestStream();

                //将数据写入该流  

                newStream.Write(bytData, 0, bytData.Length);

                //获取一个响应  

                HttpWebResponse res = (HttpWebResponse)request.GetResponse();

                //将响应写入StreamReader  

                StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);

                //读取流转换成字符串  

                string ret = sr.ReadToEnd();

                string retttt = ret.ToString();

                //释放相关非托管资源

                sr.Close();

                res.Close();

                newStream.Close();

                ret = WebServiceResponseSoapHelper.GetResponseResultStrings(ret);

                return XmlHelper.Deserialize<T>(ret);

            }

            catch (Exception ex)//抛出异常由顶层调用接收并处理

            {

                throw ex;

            }

        }

    }

2、webservice 调用帮助方法_方法参数之_requestSoapXml

    string requestSoapXml = "<?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>" +

                                  "<HelloWorld xmlns=\"http://tempuri.org/\">" +

                                      "<inputname>" + "你好世界" + "</inputname>" +

                                  "</HelloWorld>" +

                              "</soap:Body>" +

                          "</soap:Envelope>";

3、webservice 调用帮助方法_方法参数之uri

服务即wsdl地址

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢