我有一个ASP.NET网站,需要检查用户提供的增值税.所述VIES服务可用于该暴露一个SOAP API.
我需要一个关于如何使用此服务验证增值税的简单示例.在PHP中,它是以下4行:https://stackoverflow.com/a/14340495.对于C#,我发现2010年的一些文章不起作用,或者是几十行甚至几百行的"包装","辅助服务"等.
我不需要任何这些,有人可以提供类似PHP的四线程,可以在C#中检查增值税吗?谢谢.
这是一个自给自足的(没有 WCF,没有 WSDL,...)实用程序类,它将检查增值税号并获取有关公司的信息(名称和地址)。如果增值税号无效或发生任何错误,它将返回 null。
// sample calling code
Console.WriteLine(EuropeanVatInformation.Get("FR89831948815"));
...
public class EuropeanVatInformation
{
private EuropeanVatInformation() { }
public string CountryCode { get; private set; }
public string VatNumber { get; private set; }
public string Address { get; private set; }
public string Name { get; private set; }
public override string ToString() => CountryCode + " " + VatNumber + ": " + Name + ", " + Address.Replace("\n", ", ");
public static EuropeanVatInformation Get(string countryCodeAndVatNumber)
{
if (countryCodeAndVatNumber == null)
throw new ArgumentNullException(nameof(countryCodeAndVatNumber));
if (countryCodeAndVatNumber.Length < 3)
return null;
return Get(countryCodeAndVatNumber.Substring(0, 2), countryCodeAndVatNumber.Substring(2));
}
public static EuropeanVatInformation Get(string countryCode, string vatNumber)
{
if (countryCode == null)
throw new ArgumentNullException(nameof(countryCode));
if (vatNumber == null)
throw new ArgumentNullException(nameof(vatNumber));
countryCode = countryCode.Trim();
vatNumber = vatNumber.Trim().Replace(" ", string.Empty);
const string url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";
const string xml = @"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><checkVat xmlns='urn:ec.europa.eu:taxud:vies:services:checkVat:types'><countryCode>{0}</countryCode><vatNumber>{1}</vatNumber></checkVat></s:Body></s:Envelope>";
try
{
using (var client = new WebClient())
{
var doc = new XmlDocument();
doc.LoadXml(client.UploadString(url, string.Format(xml, countryCode, vatNumber)));
var response = doc.SelectSingleNode("//*[local-name()='checkVatResponse']") as XmlElement;
if (response == null || response["valid"]?.InnerText != "true")
return null;
var info = new EuropeanVatInformation();
info.CountryCode = response["countryCode"].InnerText;
info.VatNumber = response["vatNumber"].InnerText;
info.Name = response["name"]?.InnerText;
info.Address = response["address"]?.InnerText;
return info;
}
}
catch
{
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现的最简单的方法就是发送XML并在返回时对其进行解析:
var wc = new WebClient();
var request = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:ec.europa.eu:taxud:vies:services:checkVat:types"">
<soapenv:Header/>
<soapenv:Body>
<urn:checkVat>
<urn:countryCode>COUNTRY</urn:countryCode>
<urn:vatNumber>VATNUMBER</urn:vatNumber>
</urn:checkVat>
</soapenv:Body>
</soapenv:Envelope>";
request = request.Replace("COUNTRY", countryCode);
request = request.Replace("VATNUMBER", theRest);
String response;
try
{
response = wc.UploadString("http://ec.europa.eu/taxation_customs/vies/services/checkVatService", request);
}
catch
{
// service throws WebException e.g. when non-EU VAT is supplied
}
var isValid = response.Contains("<valid>true</valid>");
Run Code Online (Sandbox Code Playgroud)
在.NET平台上,通常使用Web服务以便生成代理类.这通常可以使用Visual Studio"添加Web引用"来完成,您只需填写WSDL的路径即可.另一种方法是使用wsdl.exe或svcutil.exe生成源类.
然后只需消耗这个类,验证增值税就变成了一个单行:
DateTime date = new checkVatPortTypeClient().checkVat(ref countryCode, ref vatNumber, out isValid, out name, out address);
Run Code Online (Sandbox Code Playgroud)
生成代理提供强类型API以使用整个服务,我们不需要手动创建soap信封和解析输出文本.它比您的解决方案更容易,更安全,更通用.
更新:我已将其发布为 NuGet 库。
https://github.com/TriggerMe/CSharpVatChecker
var vatQuery = new VATQuery();
var vatResult = await vatQuery.CheckVATNumberAsync("IE", "3041081MH"); // The Squarespace VAT Number
Console.WriteLine(vatResult.Valid); // Is the VAT Number valid?
Console.WriteLine(vatResult.Name); // Name of the organisation
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9150 次 |
最近记录: |