0

My application is sending some data to some government's service. The workflow is to first authenticate on their REST(JSON) service to get an authentication token, and then send the actual data+token to their SOAP service.

The problem is that if I call the authentication service in quick succession after the last soap request, their REST serice will return "404 – Not Found" HTML instead of JSON response.

This is the code for sending authentication requests:

    RestClient client = new RestClient(ret.Url);
    AuthRequestToken requestToken = new AuthRequestToken();
    requestToken.userLoginDetails.organisationCode = _organizationCode;
    requestToken.userLoginDetails.userId = _username;
    requestToken.userLoginDetails.password = _password;

    ret.RequestJson = requestToken.ToString();

    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("cache-control", "no-cache");
    request.AddParameter("application/json", ret.RequestJson, ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);

This is the code for sending SOAP requests:

    HttpWebRequest webRequest = CreateWebRequest(envelope);
    using (WebResponse webResponse = webRequest.GetResponse())
    {
      using (Stream responseStream = webResponse.GetResponseStream())
      {
        using (StreamReader rd = new StreamReader(responseStream))
        {
          ret.ResponseXML = rd.ReadToEnd();
        }
        responseStream.Close();
      }
    }

This is the CreateWebRequest() method

private HttpWebRequest CreateWebRequest(XElement content)
{
  HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_url);
  //webRequest.Headers.Add("SOAPAction", action);
  webRequest.ContentType = "text/xml;charset=\"utf-8\"";
  webRequest.Accept = "text/xml";
  webRequest.Method = "POST";

  using (Stream stream = webRequest.GetRequestStream())
  {
    content.Save(stream);
  }

  return webRequest;
}

RestClient is a class in the RestSharp library downloaded from https://restsharp.dev/

Using TcpView or netstat -abn I can see that after any request (either RestClient or HttpWebRequest), the connection stays in ESTABLISHED state for up to 5-30 seconds.

Everything works fine 99% of the time, except in a specific scenario when I make a RestClient request within 5-30 seconds after the last HttpWebRequest, before the connection switches from ESTABLISHED to CLOSE_WAIT.

I should mention that this code was working perfectly up to a couple of days ago. Before then, their authentication service was on a different IP address form their SOAP service. Now they are on the same IPAddress, and probably even on the same physical server.

Before they switched the servers I used to call authentication request before each and every SOAP request, and it worked, but since this error started happening, I modified my code to authenticate only occasionally and use the same token for a bunch of SOAP requests. This considerably reduced the chance for this error, but I still ocassinaly get it when traffic is high.

It seems to me that RestClient and HttpWebRequest are using the same socket under the hood and one of them is not cleaning up properly. It seems that RestClient inherits some junk from the HttpWebRequest because the "404 - Not Found" returned by the service looks the same as when I deliberately navigate to the wrong URL of the authentication service.

It is also possible that I'm not disposing or closing something properly, but I tried closing every stream, client or connection I could find, and injected 'using' everywhere, but nothing seems to help.

I tried contacting the government's tech suport, but judging by my prior experience, it will take weeks before they even bother to connect me to someone who can understand the problem.

This is the 404 HTML I get:

    <!doctype html>
<html lang="en">
    <head>
        <title>HTTP Status 404 – Not Found</title>
        <style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style>
    </head>
    <body>
        <h1>HTTP Status 404 – Not Found</h1>
        <hr class="line" />
        <p>
            <b>Type</b> Status Report</p>
        <p>
            <b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p>
        <hr class="line" />
        <h3>Apache Tomcat/9.0.35</h3>
    </body>
</html>

Do you have any suggestion on what I could try to prevent this from happening?

As I said, I currently have some workaround which tris to refresh the token when it gets the chance, and even delay regular requests if necessary, but Id like to not use workarounds if possible, especially since I don't know what the socket timeout is. It is 5 sec on most computers, but on some wireless networks it stays ESTABLISHED for almost a minute.

If it matters, both services are on HTTPS.

Thank you!

0

1 Answer 1

0

I solved it by making a small console application which receives credentials through command line parameters, connects to the rest service and returns a token in the standard output.

Parent application periodically calls this exe in the background, and reads a new token from the standard output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.