Web services 如何将用户代理添加到VisualStudio生成的SoapClient?

Web services 如何将用户代理添加到VisualStudio生成的SoapClient?,web-services,soap,user-agent,Web Services,Soap,User Agent,我试图在ASP MVC项目中使用Visual Studio生成的类调用Web服务,以下是我的代码: var serviceClient = new UserSoapClient("UserSoap"); var isValid = serviceClient.CheckEmail(email); ViewBag.ServiceOutput = isValid? "Email is valid" : "Email is not valid"; return View(); 但是,消息超时了,问

我试图在ASP MVC项目中使用Visual Studio生成的类调用Web服务,以下是我的代码:

var serviceClient = new UserSoapClient("UserSoap");

var isValid = serviceClient.CheckEmail(email);
ViewBag.ServiceOutput = isValid? "Email is valid" : "Email is not valid";
return View();

但是,消息超时了,问题似乎是SoapClient的请求头没有发送User-Agent属性,因为我尝试使用soapui执行相同的服务,这是唯一的区别。是否有任何方法可以设置用户代理,因为我正在查看文档,它似乎不是很明显。

我在尝试使用SoapClient的服务时遇到了类似的问题。我能够通过浏览器从服务获得响应,但当我在项目服务中执行代码时,返回协议异常,远程服务器返回错误:(502)坏网关。信息

经过几个小时的跟踪,我意识到异常是由用户代理定义引起的。我试图将用户代理添加到Soap请求头中,但没有办法。不能将用户代理添加到注册为服务引用的Soap请求中。但可以为注册为Web服务引用的请求定义用户代理头

我的解决方案是:

  • 从项目中删除服务引用
  • 从“添加服务引用”窗口的“高级”菜单中添加与Web引用相同的引用
  • 创建一个新对象并定义UserAgent属性,如下所示

    var svc = new ServiceReference.QueryService();
    svc.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36";
    

  • 是否使用了“添加服务引用”?是的,UserSoapClient是由包含对web服务方法的调用的AddServicereference生成的类