I'm trying to consume a WebService
but I have the following error:
{"Error on the remote server: (500) Internal server error."};
The address URL
of WebService
is the following link .
Through a Reference I could solve it, what I'm looking for is to do it with code .
public string EnviarXML()
{
string vResult = "";
string cFileNameZip = Path.GetFileName(cRutaFileZip);
string cfilebyt = Convert.ToBase64String(File.ReadAllBytes(cRutaFileZip));
byte[] vRequestDataXml = CreateHttpRequestXmlEnvio(cFileNameZip, cfilebyt);
vResult = CallWebMethod(vRequestDataXml);
return vResult;
}
public string CallWebMethod(byte[] valRequestData)
{
try
{
//byte[] vRequestData = valRequestData;
HttpWebRequest vhttpRequest = (HttpWebRequest)HttpWebRequest.Create(cWebServiceURL);
vhttpRequest.AllowWriteStreamBuffering = false;
vhttpRequest.Headers.Add("SOAPAction", "sendBill");
vhttpRequest.Method = "POST";
vhttpRequest.ContentType = "text/xml; charset=UTF-8";
vhttpRequest.Accept = "text/xml";
vhttpRequest.ContentLength = valRequestData.Length;
vhttpRequest.ProtocolVersion = HttpVersion.Version11;
vhttpRequest.Credentials = CredentialCache.DefaultCredentials;
vhttpRequest.Timeout = 30000;
vhttpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.17929)";
vhttpRequest.GetRequestStream().Write(valRequestData, 0, valRequestData.Length);
string vResponse = string.Empty;
HttpWebResponse vhttpResponse = null;
vhttpResponse = (HttpWebResponse)vhttpRequest.GetResponse();
System.IO.Stream vbaseStream = vhttpResponse.GetResponseStream();
System.IO.StreamReader vResponseStreamReader = new System.IO.StreamReader(vbaseStream);
vResponse = vResponseStreamReader.ReadToEnd();
vResponseStreamReader.Close();
return vResponse;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private byte[] CreateHttpRequestXmlEnvio(string cFileName, string cFileBase64)
{
UTF8Encoding vEncoding = new UTF8Encoding();
string vxml = "<soapenv:Envelope xmlns:ser=\"http://service.sunat.gob.pe/\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"+
"<soapenv:Header>"+
"<wsse:Security>"+
"<wsse:UsernameToken>"+
"<wsse:Username>" + cUserSol + "</wsse:Username>" +
"<wsse:Password>" + cPwdSol + "</wsse:Password>"+
"</wsse:UsernameToken>"+
"</wsse:Security>"+
"</soapenv:Header>"+
"<soapenv:Body>"+
"<ser:sendBill>"+
"<fileName>" + cFileName + "</fileName>"+
"<contentFile>" + cFileBase64 + "</contentFile>"+
"</ser:sendBill>"+
"</soapenv:Body>"+
"</soapenv:Envelope>";
return vEncoding.GetBytes(vxml);
}