解決.NET WebService引用后添加HTTP Header的問題


麻蛋,搜索了好久,找到的都是對soap header的操作,不是對WebService的HTTP Header的操作,這是兩種不同的概念,平常我們發起的WebService請求走的都是http通信協議,POST方式的請求,請求體是發送對象的SOAP結構。算了直接上答案。只不過還是英語不好

http://stackoverflow.com/questions/897782/how-to-add-custom-http-header-for-c-sharp-web-service-client-consuming-axis-1-4

上面是源地址,貌似有兩種方法,一種是排名第一的

添加一下代碼:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
  System.Net.WebRequest request = base.GetWebRequest(uri);
  request.Headers.Add("myheader", "myheader_value");
  return request;
}

網上查了一天愣是沒有找到有用的完整的demo,費了半天勁,實在找不到該插入那個地方啊,試遍了自動生成的Reference.cs里的任何位置,都不對,關鍵是winform 里面引用完成接口之后還找不到Reference.cs 文件。

第二種方式:最好是把using 去掉,要不出錯都不知道哪里報錯了。

static void Main(string[] args)
       {
           //wsAuth:就是自動生成的WebService服務的別名
           using (wsAuth.WsAuthenticationClient client = new wsAuth.WsAuthenticationClient())
           {
               using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
               {
                   var httpRequestProperty = new HttpRequestMessageProperty();
                   httpRequestProperty.Headers["ws-lala"] = "111";
                   httpRequestProperty.Headers["ws-lala11"] = "222";
                   OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
 
                   var cc = client.login("tlzzu", "123456");
               }
           }
       }

  去掉了using之后的測試實例,親測有用

public string ces(string xml)
        {
            try
            {
                ceshi.RegistryAddRequestImplServiceClient client = new ceshi.RegistryAddRequestImplServiceClient();
                OperationContextScope scope = new OperationContextScope(client.InnerChannel);
                var httpRequestProperty = new HttpRequestMessageProperty();
                httpRequestProperty.Headers["username"] = "py";
                httpRequestProperty.Headers["password"] = "py";
                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
                var cc = client.HIPMessageServer("PatientRegistryAddRequest", xml);
            }
            catch (Exception ec)
            {

            }

            return "";
        }

  以上方式方法最后生成的xml   Headers 表頭的樣式是,在xml 最上面, Headers:{內容}

有可能不是想要的,有可能想要的是< Headers><username></username></ Headers> 這種 。這樣的話就要用下面提供的方法了

經過測試可用:

新建一個類文件:取名WSHelper.cs   “名字隨意”

里面添加這么一個方法屬性:

public class RequestSOAPHeader : System.Web.Services.Protocols.SoapHeader
        {
            public string username { get; set; }
            public string password { get; set; }
        }

  這個方法里面定義Headers 里面的內容   Headers 一般都是表頭驗證,一般存用戶名和密碼  這樣的生成完之后就是:< Headers><username></username><password></password></ Headers>

 

下面是具體的方法;

        public string webserivice(object obj, string path)
        {
            try
            {
                string xml1 = string.Empty;
                RegistraAddRequest.RegistryAddRequestImplServiceClient client = new RegistraAddRequest.RegistryAddRequestImplServiceClient();
                OperationContextScope scope = new OperationContextScope(client.InnerChannel);

                //實例化剛才創建的類文件,並且給類里面的兩個屬性賦值。
                WSHelper.RequestSOAPHeader RequestSOAPHeader = new WSHelper.RequestSOAPHeader();
                RequestSOAPHeader.username = "xiaoming";
                RequestSOAPHeader.password = "8504051434B74359A005E640AF40A8CD";
                //創建具有指定數據的新消息頭。
                MessageHeader aMessageHeader = MessageHeader.CreateHeader("RequestSOAPHeader", "http://tempuri.org", RequestSOAPHeader);
                //將指定的消息頭添加到集合
                OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);

                //以上是引用接口添加Header表頭驗證的方法,下面都是我業務需要自己的代碼了。
                XDocument document = XDocument.Load(path);
                string xml = base64jm(document.ToString());
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(GenerateXml._basePath + "\\xml模板\\waibuModel.xml");
                Entity.CheckReport entity = obj as Entity.CheckReport;
                xml1 = xmlDoc.InnerXml.Replace("[XML內容]", xml);
                xml1 = xml1.Replace("[全球唯一標識]", Guid.NewGuid().ToString("D"));
                xml1 = xml1.Replace("[身份證號]", entity.IdCardCode);
                xml1 = xml1.Replace("[患者姓名]", entity.name);
                xml1 = xml1.Replace("[卡號]", entity.specimenCode);
                xml1 = xml1.Replace("[醫院編碼]", entity.fileSaveOrganizationCode);
                xml1 = xml1.Replace("[醫院名稱]", entity.fileSaveOrganizationName);
                xml1 = xml1.Replace("[省份]", "暫無");
                xml1 = xml1.Replace("[地市]", "暫無");
                xml1 = xml1.Replace("[市縣]", "暫無");
                xml1 = xml1.Replace("[鄉鎮]", "暫無");
                xml1 = xml1.Replace("[街道]", "暫無");
                xml1 = xml1.Replace("[小區]", "暫無");
                xml1 = xml1.Replace("[時間]", entity.diagnoseDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"));
                xml1 = xml1.Replace("[服務名稱]", entity.checkWayName + "服務");
                xml1 = xml1.Replace("[醫生姓名]", entity.checkDoctorCode);
                // 得到根節點bookstore

                // 得到根節點的所有子節點

                var cc = client.HIPMessageServer("ProvideAndRegisterDocumentSet-b", xml1);
            }
            catch (Exception ec)
            {

            }
            return "";
        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM