Httpwebresponde format return

Asked

Viewed 52 times

0

I have the following Code:

try
{
  public string ProcessAttachment(string fileInput)
  {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://flnws001qae.nexxera.com:80/nexxeraws/v2/SkylineWSv2");
    req.Method = "POST";
    req.ProtocolVersion = HttpVersion.Version11;
    req.Headers.Add("Accept-Encoding", "gzip,deflate");
    req.ContentType = "multipart/related; type=\"text/xml\"; start=\"teste\"; boundary=\"----=_Part_72_348989292.1565031692584\"";
    req.Headers.Add("SOAPAction", "\"\"");
    req.Headers.Add("MIME-Version", "1.0");
    //req.ContentLength = 1854;
    req.Host = "flnws001qae.nexxera.com:80";
    req.UserAgent = "Apache-HttpClient/4.1.1 (java 1.5)";        
    req.KeepAlive = true; 

    System.Net.ServicePointManager.Expect100Continue = false;
    Stream memStream = new System.IO.MemoryStream();
    FileStream fileStream = new FileStream(fileInput, FileMode.Open, FileAccess.Read);
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
    {
        memStream.Write(buffer, 0, bytesRead);
    }
    fileStream.Close();
    Stream stm = req.GetRequestStream();
    memStream.Position = 0;
    byte[] tempBuffer = new byte[memStream.Length];
    memStream.Read(tempBuffer, 0, tempBuffer.Length);
    memStream.Close();
    stm.Write(tempBuffer, 0, tempBuffer.Length);
    stm.Close();
    HttpWebResponse resp = null;
    resp = (HttpWebResponse)req.GetResponse();
    stm = resp.GetResponseStream();
    StreamReader r = new StreamReader(stm);
    return r.ReadToEnd();
   }
 }
 catch (WebException ex)
 {
   string exMessage = ex.Message;
   throw ex;
  }
} 

I have the following return:

Remote server returned an error: (500) Internal Server Error.

There is a way to get back Webservice, for example " Authentication failure!" In the soapui I have the return:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
   <env:Fault>
     <faultcode xmlns:codeNS="http://schemas.xmlsoap.org/soap/envelope/">codeNS:Server</faultcode>
     <faultstring>1090 - Falha de autenticação!</faultstring>
  </env:Fault>
 </env:Body>
 </env:Envelope>

1 answer

0

To have more details of the error/fault returned, the WebException has an object Response, who has more information than occurred on the call from the service.

You could test if it has value in Response and check the information like this:

try
{
  // código que chama o serviço
}
catch (WebException ex)
{
   if (ex.Response != null)
   {
       var codigoErro    = (HttpWebResponse)e.Response).StatusCode;
       var descricaoErro = (HttpWebResponse)e.Response).StatusDescription;
   } 
}

The documentation is here: https://docs.microsoft.com/en-us/dotnet/api/system.net.webexception.response?view=netframework-4.8#System_Net_WebException_Response

When it’s wcf there’s a way to directly catch the faultexception which may work too, but I’m not sure, so I leave it here as an alternative to be tested:

catch (WebException ex)
{
   var mensagemFault = ex.CreateMessageFault();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.