简体   繁体   English

C#Web服务客户端:如何添加自定义标头以进行请求?

[英]c# web service client: how to add custom header to request?

to access a web service i created a proxy class using visual studio "add service reference". 为了访问Web服务,我使用Visual Studio“添加服务参考”创建了一个代理类。 Unfortunatly i have to put in the soap header the followings elements 不幸的是,我必须在肥皂标题中放入以下元素

<soapenv:Header>
  <ser:CF>XXXXXXXXXX</ser:CFSender>
        <ser:Identity xmlns="http://company.org" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
     <ser:AppKey>XXX</ser:AppKey>
     <ser:AppName>XXXX</ser:AppName>
     <ser:Parameter>Y</ser:Parameter>
  </ser:Identity>
 </soapenv:Header>

The proxy generated by visual studio has only che following element as method parameter Visual Studio生成的代理仅将以下元素作为方法参数

<ser:CF>XXXXXXXXXX</ser:CFSender>

but not the identity. 但不是身份。 I need to put "Identity" element just before the web service invocation programmatically.... i need the simplest solution. 我需要以编程方式在Web服务调用之前放置“ Identity”元素。...我需要最简单的解决方案。 I saw in other questions how to put one parameter...but identity is a nested object and i don't know what to do. 我在其他问题中看到了如何放置一个参数...但是身份是一个嵌套对象,我不知道该怎么办。 Anyone can help? 有人可以帮忙吗?

Here is the solution, from http://blogs.msdn.com/b/wsdevsol/archive/2014/02/07/adding-custom-messageheader-and-http-header-to-a-wcf-method-call.aspx (use Identity obj instead of userInfo) 这是解决方案,来自http://blogs.msdn.com/b/wsdevsol/archive/2014/02/07/adding-custom-messageheader-and-http-header-to-a-wcf-method-call。 aspx (使用Identity obj代替userInfo)

using(new OperationContextScope(client.InnerChannel)) {

// We will use a custom class called UserInfo to be passed in as a MessageHeader

UserInfo userInfo = new UserInfo();

userInfo.FirstName = "John";

userInfo.LastName = "Doe";

userInfo.Age = 30;



// Add a SOAP Header to an outgoing request

MessageHeader aMessageHeader = MessageHeader.CreateHeader("UserInfo", "http://tempuri.org", userInfo);

OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为使用Axis 1.4 Web服务的C#Web服务客户端添加自定义Http Header - How to add custom Http Header for C# Web Service Client consuming Axis 1.4 Web service 如何配置C#Web服务客户端以并行发送HTTP请求标头和正文? - How do I configure a C# web service client to send HTTP request header and body in parallel? 如何在 WCF web 服务 ZD7EFA19FBE7D3972FD5ADB602422 中请求之前创建自定义 header - How to create custom header before request in WCF web service C# 将C#请求Web服务与标头和正文一起使用 - Use C# request web service with header and body 如何在HTTP请求c#中添加一个header? - How to add a header in HTTP request c#? Java客户端对Web服务(c#)的用户名和密码请求 - java client request to web-service(c#) with username and password 如何使用C#将自定义标头添加到SOAP请求中? - How do I add a custom header to a SOAP request using C#? 如何在c#的客户端请求中插入此soap标头 - how can insert this soap header in client side request at c# 如何修改请求的http头; C#中的Web参考 - how to modify http header of request; web reference in C# 如何从Java客户端向C#Web服务发送数据 - How to send data to c# web service from java client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM