• .NET 请求JAVA接口带SOAP验证 AXIS2


    /// <summary>
            /// 请求JAVA接口带SOAP验证
            /// </summary>
            /// <param name="URL"> 请求URL</param>
            /// <param name="MethodName"> 请求方法名 </param>
            /// <param name="Pars"> 请求方法参数 </param>
            /// <returns> string</returns>
            public static string GetData( String URL, String MethodName, Hashtable Pars)
            {
                try
                {
                    String xml = "<?xml version='1.0' encoding='utf-8'?>" ;
                    xml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" ;
                    xml += "<soapenv:Header>" ;
                    xml += "<wsse:Authentication xmlns:wsse=\"http://handler.com\">" ;
                    xml += "<wsse:Username>toone</wsse:Username>" ;
                    xml += "<wsse:Password>111111</wsse:Password>" ;
                    xml += "</wsse:Authentication>" ;
                    xml += "</soapenv:Header>" ;
                    xml += "<soapenv:Body>";
                    xml += String.Format("<{0} xmlns=\"http://services.mmc.com\">" , MethodName);
                    foreach (System.Collections.DictionaryEntry ht in Pars)
                    {
                        xml += String.Format("<{0} xmlns=\"\">{1}</{0}>" , ht.Key, ht.Value);
                    }
                    xml += "<languageType>.net</languageType>" ;
                    xml += String.Format("</{0}>" , MethodName);
                    xml += "</soapenv:Body>";
                    xml += "</soapenv:Envelope>" ;
     
     
                    HttpWebRequest req = (HttpWebRequest )WebRequest.Create( String.Format("{0}/{1}" , URL, MethodName));
                    req.Headers.Add( "SOAPAction", URL);
                    req.ContentType = "text/xml;charset=\"utf-8\"" ;
                    req.Accept = "text/xml";
                    req.Method = "POST";
     
                    //是否和请求一起发送
                    req.UseDefaultCredentials = true;
                    //写数据信息的流对象
                    using (StreamWriter swMessages = new StreamWriter(req.GetRequestStream()))
                    {
                        //写入的流以XMl格式写入
                        swMessages.Write(xml);
                        //关闭写入流
                        swMessages.Close();
                    }
                    HttpWebResponse res;
                    try
                    {
                        res = ( HttpWebResponse)req.GetResponse();
                    }
                    catch (WebException ex)
                    {
                        res = ( HttpWebResponse)ex.Response;
                    }
                    StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("utf-8" ));
                    String strHtml = sr.ReadToEnd();
                    return strHtml;
                }
                catch (Exception e)
                {
                    throw new Exception(e.ToString());
                }
            }
     
     
    调用 方法
     
       string url = "http://192.168.1.31:8080/webapi/services/StudentService" ;
                Hashtable ht = new Hashtable();
                ht.Add( "name", "xiaoming" );
                this.richTextBox1.Text=GetData(url, "getXmlDataByName" , ht);
  • 相关阅读:
    使用QOAuth来进行新浪/腾讯微博验证(二)
    很不错的Utility库,C#4扩展 各种功能齐全,两行代码搞定图片转字符
    使用QOAuth来进行新浪/腾讯微博验证(一)
    可怜的小猪&香农熵
    消息队列MQ如何保证消息不丢失
    40 亿个 QQ 号码如何去重,bitmap去重
    参数的设置
    自动化测试的十个要点
    LR学习中的一个低级错误
    Windows下用CMake编译libuv
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/2731033.html
Copyright © 2020-2023  润新知