HTTP SOAP Request

public string SoapRequest(string url, string message, string type, Encoding encoding)
        {
            string result = string.Empty;
            Stream reqstr = null;
            System.IO.Stream responseStream = null;
            System.IO.StreamReader reader = null;
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = type.ToString();
                request.ContentType = "application/soap+xml;charset=UTF-8;action=\"" + url + "\"";
                request.Headers.Add("SOAPAction", url);
                
                if (type.ToString().ToLower() == "post")
                {
                    reqstr = request.GetRequestStream();
                    byte[] buff = encoding.GetBytes(message);
                    reqstr.Write(buff, 0, buff.Length);
                    reqstr.Flush();
                    reqstr.Close();
                    reqstr.Dispose();
                    reqstr = null;
                }
                // 接收返回的页面
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                responseStream = response.GetResponseStream();
                reader = new System.IO.StreamReader(responseStream, encoding);
                result = reader.ReadToEnd();
                if (OnStateComplate != null)
                    OnStateComplate(result, fspc);
                if (OnReplyComplate != null)
                    OnReplyComplate(result, fspc);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (reqstr != null)
                {
                    reqstr.Flush();
                    reqstr.Close();
                    reqstr.Dispose();
                }
                if (responseStream != null)
                {
                    responseStream.Flush();
                    responseStream.Close();
                    responseStream.Dispose();
                }
            }
            
        }

 

public static string GetStringSOAP1(Hashtable ht,string target)
        {
            StringBuilder body = new StringBuilder();
            body.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            body.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
            body.Append("<soap:Body>");
            body.Append("<"+target+" xmlns=\"http://service.pubinfo.com.cn\">");
            body.AppendFormat("<in0>{0}</in0>", ht["loginName"].ToString());
            body.AppendFormat("<in1>{0}</in1>", ht["loginPWD"].ToString());
            int index = 2;
            if (ht.ContainsKey("mobiles") && !string.IsNullOrEmpty(ht["mobiles"].ToString()))
            {
                body.AppendFormat("<in{1}>{0}</in{1}>", ht["mobiles"].ToString(),index++);
            }
            if (ht.ContainsKey("content") && !string.IsNullOrEmpty(ht["content"].ToString()))
            {
                body.AppendFormat("<in{1}>{0}</in{1}>", ht["content"].ToString(), index++);
            }
            if (ht.ContainsKey("sendNo")  && !string.IsNullOrEmpty(ht["sendNo"].ToString()))
            {
                body.AppendFormat("<in{1}>{0}</in{1}>", ht["sendNo"].ToString(), index++);
            }
            body.Append("</"+target+"></soap:Body></soap:Envelope>");
            return body.ToString();
        }

 

你可能感兴趣的:(HTTP SOAP Request)