The webservice DownloadNFe has been deactivated according to NT 2014.002.v.1.02.
The webservice you should use is:
https://www1.nfe.fazenda.gov.br/NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx
In this webservice you make the manifestation of the recipient and in the next query it returns the complete NFe XML.
Follow the link of the technical note: Technical Note 2014.002 - v1.02
I made a very simple example, I put a button and a textbox in a form. I added the webservice address in the visual studio project as ServiceReference, so visual studio already generates the necessary classes. Here is the code:
Do not forget to replace the code data with your company data, and without reading the technical note you can not understand the return of the webservice.
private void button1_Click(object sender, EventArgs e)
{
NFeDistribuicaoDFeSoapClient consDFe = new NFeDistribuicaoDFeSoapClient();
consDFe.ClientCredentials.ClientCertificate.Certificate = ObterDoRepositorio();
distDFeInt distDfe = new distDFeInt();
distDfe.versao = TVerDistDFe.Item100;
distDfe.tpAmb = TAmb.Item1;
distDfe.cUFAutor = TCodUfIBGE.Item50;
distDfe.ItemElementName = ItemChoiceType.CNPJ;
distDfe.Item = "000000000000000";
distDFeIntDistNSU distNSU = new distDFeIntDistNSU();
distNSU.ultNSU = "000000000";
distDfe.Item1 = distNSU;
string xmlEnvio = SerializeToString(distDfe);
var removeq1 = new string[] { ":q1", "q1:" };
foreach (var item in removeq1)
{
xmlEnvio = xmlEnvio.Replace(item, string.Empty);
}
XElement xml = XElement.Parse(xmlEnvio);
var asd = consDFe.nfeDistDFeInteresse(xml);
textBox1.Text = asd.ToString();
}
public static X509Certificate2 ObterDoRepositorio()
{
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.MaxAllowed);
var collection = store.Certificates;
var fcollection = collection.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
var scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Certificados válidos:", "Selecione o certificado que deseja usar",
X509SelectionFlag.SingleSelection);
if (scollection.Count == 0)
{
throw new Exception("Nenhum certificado foi selecionado!");
}
store.Close();
return scollection[0];
}
public static string SerializeToString(Object value)
{
var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var serializer = new XmlSerializer(value.GetType());
var settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (var stream = new StringWriter())
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, value, emptyNamepsaces);
return stream.ToString();
}
}
Link in github