Esempio n. 1
0
        /// <summary>
        /// 获取命名空间
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        private static string GetNamespace(String URL)
        {
            string      ret;
            XmlDocument doc = new XmlDocument();

            try
            {
                //HTTP代理或无代理方式
                if (_proxyType == ProxyType.None || _proxyType == ProxyType.Http)
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
                    SetWebRequest(request);
                    WebResponse  response = request.GetResponse();
                    StreamReader sr       = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    doc.LoadXml(sr.ReadToEnd());
                    sr.Close();
                }
                else//SOCKS4/5代理
                {
                    Socket sRH = null;

                    byte[] buffer;
                    int    rlength = 0;
                    string sr;

                    Uri uri = new Uri(URL + "?WSDL");

                    //连接SOCKS代理取得套接字
                    if (_proxyType == ProxyType.Socks4)
                    {
                        sRH = SocksProxy.ConnectSocks4Server(
                            _proxyServer,
                            _proxyPort,
                            uri.Host,
                            uri.Port);
                    }
                    else
                    {
                        sRH = SocksProxy.ConnectSocks5Server(
                            _proxyServer,
                            _proxyPort,
                            uri.Host,
                            uri.Port,
                            _proxyAccount,
                            _proxyPassword);
                    }
                    try
                    {
                        sRH.SendTimeout    = 10000;
                        sRH.ReceiveTimeout = 10000;
                        SSend("GET " + uri.PathAndQuery + " HTTP/1.0\r\n", sRH);
                        SSend("Connection:close\r\n", sRH);
                        SSend("Host:" + uri.Host + "\r\n", sRH);
                        SSend("Content-Type: text/xml; charset=utf-8\r\n", sRH);
                        SSend("User-agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n", sRH);
                        SSend("Accept-language:zh-cn\r\n", sRH);
                        SSend("\r\n", sRH);

                        buffer  = new byte[1024];
                        rlength = sRH.Receive(buffer);
                        sr      = Encoding.Default.GetString(buffer, 0, rlength);
                        ret     = sr;
                        while (sr.Length > 0)
                        {
                            rlength = sRH.Receive(buffer);
                            sr      = Encoding.Default.GetString(buffer, 0, rlength);
                            ret    += sr;
                        }
                        if (ret.Length > 0)
                        {
                            string response = ret.Substring(ret.IndexOf("<?xml"));
                            doc.LoadXml(response);
                        }
                    }
                    finally
                    {
                        sRH.Shutdown(SocketShutdown.Both);
                        sRH.Close();
                    }
                }
                XmlNode node = doc.SelectSingleNode("//@targetNamespace");
                if (node == null)
                {
                    throw new Exception("服务地址可能有误,请检查地址!");
                }
                else
                {
                    ret = doc.SelectSingleNode("//@targetNamespace").Value;
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("timeout") ||
                    ex.Message.Contains("timed out"))
                {
                    throw new Exception(PRO_ReceiptsInvMgr.Resources.Message.TimeOutError);
                }
                else if (ex.Message.Contains("No connection could be made") ||
                         ex.Message.Contains("Unable to connect"))
                {
                    throw new Exception(PRO_ReceiptsInvMgr.Resources.Message.ConnectError);
                }
                else if (ex.Message.Contains("Internal Server Error"))
                {
                    throw new Exception(PRO_ReceiptsInvMgr.Resources.Message.InternalError);
                }
                else
                {
                    throw;
                }
            }
            return(ret);
        }
Esempio n. 2
0
        private static Hashtable _xmlNamespaces = new Hashtable();//缓存xmlNamespace,避免重复调用GetNamespace

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        private static void DownLoadFile(string url, string file)
        {
            try
            {
                //HTTP代理或无代理方式
                if (_proxyType == ProxyType.None)
                {
                    WebClient client = new WebClient();
                    client.DownloadFile(url, file);
                    client.Dispose();
                }
                else if (_proxyType == ProxyType.Http)
                {
                    System.Net.WebProxy proxy = new System.Net.WebProxy(_proxyServer + ":" + _proxyPort, true);
                    if (_proxyAccount.Trim().Length > 0 && _proxyPassword.Trim().Length > 0)
                    {
                        proxy.Credentials = new System.Net.NetworkCredential(_proxyAccount, _proxyPassword, "");
                    }
                    else
                    {
                        proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    }

                    WebClient client = new WebClient();
                    client.Proxy = proxy;
                    client.DownloadFile(url, file);
                    client.Dispose();
                }
                else
                {
                    Socket sRH = null;

                    Uri uri = new Uri(url);

                    //连接SOCKS代理取得套接字
                    if (_proxyType == ProxyType.Socks4)
                    {
                        sRH = SocksProxy.ConnectSocks4Server(
                            _proxyServer,
                            _proxyPort,
                            uri.Host,
                            uri.Port);
                    }
                    else
                    {
                        sRH = SocksProxy.ConnectSocks5Server(
                            _proxyServer,
                            _proxyPort,
                            uri.Host,
                            uri.Port,
                            _proxyAccount,
                            _proxyPassword);
                    }

                    MemoryStream result = new MemoryStream(10240);
                    try
                    {
                        byte[] send = GetSendHeaders(uri, "", "");

                        if (sRH.Connected)
                        {
                            sRH.Send(send, SocketFlags.None);
                            ProcessData(sRH, ref result);
                        }

                        result.Flush();
                    }
                    finally
                    {
                        sRH.Shutdown(SocketShutdown.Both);
                        sRH.Close();
                        result.Dispose();
                    }

                    result.Seek(0, SeekOrigin.Begin);

                    FileStream fs = File.Create(file);
                    try
                    {
                        byte[] data = result.ToArray();
                        fs.Write(data, 0, data.Length);
                        fs.Close();
                    }
                    catch (Exception ex)
                    {
                        Logging.Log4NetHelper.Error(typeof(WebServiceCaller), ex);
                    }

                    sRH.Close();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("timeout") ||
                    ex.Message.Contains("timed out"))
                {
                    throw new Exception(PRO_ReceiptsInvMgr.Resources.Message.TimeOutError);
                }
                else if (ex.Message.Contains("No connection could be made") ||
                         ex.Message.Contains("Unable to connect"))
                {
                    throw new Exception(PRO_ReceiptsInvMgr.Resources.Message.ConnectError);
                }
                else if (ex.Message.Contains("Internal Server Error"))
                {
                    throw new Exception(PRO_ReceiptsInvMgr.Resources.Message.InternalError);
                }
                else
                {
                    throw;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 调用Service方法
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="MethodName"></param>
        /// <param name="paramName"></param>
        /// <param name="paramValue"></param>
        /// <param name="XmlNs"></param>
        /// <returns></returns>
        private static string QuerySoapWebService(String URL, String MethodName, string paramName, string paramValue, string XmlNs)
        {
            _xmlNamespaces[URL] = XmlNs;//加入缓存,提高效率
            String      retXml;
            String      retX;
            XmlDocument doc = new XmlDocument();

            try
            {
                //设置SOAP信息
                doc.LoadXml("<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:Envelope>");

                AddDelaration(doc);
                XmlElement soapBody = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");

                //设置SOAP数据
                XmlElement soapMethod = doc.CreateElement(MethodName);
                soapMethod.SetAttribute("xmlns", XmlNs);
                if (!string.IsNullOrEmpty(paramName))
                {
                    XmlElement soapPar = doc.CreateElement(paramName);
                    soapPar.InnerXml = ObjectToSoapXml(paramValue);
                    soapMethod.AppendChild(soapPar);
                }
                soapBody.AppendChild(soapMethod);
                doc.DocumentElement.AppendChild(soapBody);

                string postData = doc.OuterXml;
                //HTTP代理或无代理方式
                if (_proxyType == ProxyType.None || _proxyType == ProxyType.Http)
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
                    request.Method      = "POST";
                    request.ContentType = "text/xml; charset=utf-8";
                    request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");
                    SetWebRequest(request);

                    byte[] data = Encoding.UTF8.GetBytes(postData);
                    request.ContentLength = data.Length;

                    //POST数据
                    Stream writer = request.GetRequestStream();
                    writer.Write(data, 0, data.Length);
                    writer.Close();

                    //取得调用结果
                    WebResponse  response = request.GetResponse();
                    StreamReader sr       = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    retX = sr.ReadToEnd();

                    doc.LoadXml(retX);
                    sr.Close();
                }
                else
                {
                    Socket sRH = null;

                    byte[] buffer;
                    int    rlength = 0;
                    string sr;

                    Uri uri = new Uri(URL + "?WSDL");

                    //连接SOCKS代理取得套接字
                    if (_proxyType == ProxyType.Socks4)
                    {
                        sRH = SocksProxy.ConnectSocks4Server(
                            _proxyServer,
                            _proxyPort,
                            uri.Host,
                            uri.Port);
                    }
                    else
                    {
                        sRH = SocksProxy.ConnectSocks5Server(
                            _proxyServer,
                            _proxyPort,
                            uri.Host,
                            uri.Port,
                            _proxyAccount,
                            _proxyPassword);
                    }
                    try
                    {
                        sRH.SendTimeout    = 10000;
                        sRH.ReceiveTimeout = 10000;
                        //发送数据
                        SSend("POST " + uri.AbsolutePath + " HTTP/1.0\r\n", sRH);
                        SSend("Connection:close\r\n", sRH);
                        SSend("Host:" + uri.Host + "\r\n", sRH);
                        SSend("Content-Type: text/xml; charset=utf-8\r\n", sRH);
                        SSend("User-agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n", sRH);
                        SSend("Accept-language:zh-cn\r\n", sRH);
                        SSend("Content-Length:" + postData.Length + "\r\n", sRH);
                        SSend("SOAPAction:" + "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"\r\n", sRH);
                        SSend("\r\n", sRH);
                        SSend(postData, sRH);

                        //接收数据
                        buffer  = new byte[1024];
                        rlength = sRH.Receive(buffer);
                        sr      = Encoding.Default.GetString(buffer, 0, rlength);
                        string ret = sr;
                        while (sr.Length > 0)
                        {
                            rlength = sRH.Receive(buffer);
                            sr      = Encoding.Default.GetString(buffer, 0, rlength);
                            ret    += sr;
                        }
                        if (ret.Length > 0)
                        {
                            string b = ret.Substring(ret.IndexOf("<soap:"));
                            doc.LoadXml(b);
                        }
                    }
                    finally
                    {
                        sRH.Shutdown(SocketShutdown.Both);
                        sRH.Close();
                    }
                }
                XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
                mgr.AddNamespace("soap", PRO_ReceiptsInvMgr.Resources.Common.WebServiceSoapUri);
                retXml = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("timeout") ||
                    ex.Message.Contains("timed out"))
                {
                    throw new Exception("连接服务器超时,请重试!");
                }
                else if (ex.Message.Contains("No connection could be made") ||
                         ex.Message.Contains("Unable to connect"))
                {
                    throw new Exception("连接服务器失败,请稍候重试!");
                }
                else if (ex.Message.Contains("Internal Server Error"))
                {
                    throw new Exception("服务器端程序运行出现错误,请联系服务器端管理员!");
                }
                else
                {
                    throw;
                }
            }

            return(retXml);
        }