简体   繁体   English

C#:通过服务引用调用Web服务:缺少内容类型标头

[英]C#: Calling web service through service reference: Content-Type Header missing

I'm trying to access http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl through C#. 我正在尝试通过C#访问http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

I have added it as a Service Reference. 我已将其添加为服务参考。 Now i have the following code: 现在我有以下代码:

public static string checkVat(string _countryCode, string _vatNumber, string _companyName, bool _isValid, string _companyAdress)
    {
        var binding = new BasicHttpBinding();
        var endpointAddress = new EndpointAddress("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
        var client = new ServiceReferenceCheckVat.checkVatPortTypeClient(binding, endpointAddress);

        string response = client.checkVat(ref _countryCode, ref _vatNumber, out _isValid, out _companyName, out _companyAdress);

        return response;
    }

This line: 这行:

string response = client.checkVat(ref _countryCode, ref _vatNumber, out _isValid, out _companyName, out _companyAdress);

is now throwing following exception : 现在抛出以下异常

An exception of type 'System.ServiceModel.ProtocolException' occurred in mscorlib.dll but was not handled in user code mscorlib.dll中发生类型'System.ServiceModel.ProtocolException'的异常,但未在用户代码中处理

Additional information: An HTTP Content-Type header is required for SOAP messaging and none was found. 附加信息:SOAP消息传递需要HTTP Content-Type标头,但没有找到。

Edit: I call this class from X++, if that makes any difference. 编辑:如果有什么区别,我从X ++调用此类。

Maybe you need to add setting to your web config. 也许您需要将设置添加到您的Web配置中。 It has to be add automaticly when you add this web service. 添加此Web服务时,必须自动添加它。 I tried and It retuned data to me. 我尝试过,它重新为我调整了数据。

  <setting name="WebApplication6_eu_europa_ec_checkVatService" serializeAs="String">
    <value>http://ec.europa.eu/taxation_customs/vies/services/checkVatService</value>
  </setting>

and this may help you this 这可能会帮助你这个

can you try this? 你可以试试这个吗?

    public static string checkVat(string _countryCode, string _vatNumber, string _companyName, bool _isValid, string _companyAdress)
    {
        var client = new eu.europa.ec.checkVatService();
        return client.checkVat(ref _countryCode, ref _vatNumber, out _isValid, out _companyName, out _companyAdress).ToString();
    }

Got it to work. 得到它的工作。 The problem was a wrong endpoint adress. 问题是端点地址错误。

Instead of using http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl (from which the referenced service was created from), you have to use http://ec.europa.eu/taxation_customs/vies/services/checkVatService 不必使用http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl (从中创建引用服务的位置),而必须使用http://ec.europa.eu/taxation_customs/vies/services / checkVatService

This link is also automatically used in the app.config file. 这个链接也会在app.config文件中自动使用。 It works but for some reason it looks kinda wrong. 它可以工作,但是由于某种原因,看起来有点不对劲。 If someone can explain it more, that would be great. 如果有人可以解释得更多,那就太好了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM