简体   繁体   中英

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#.

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

Additional information: An HTTP Content-Type header is required for SOAP messaging and none was found.

Edit: I call this class from X++, if that makes any difference.

Maybe you need to add setting to your web config. It has to be add automaticly when you add this web service. 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

This link is also automatically used in the app.config file. It works but for some reason it looks kinda wrong. If someone can explain it more, that would be great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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