2

I'm using WinForms and accessing my Restful webservice. For some reason the code after a while breaks, by giving the timeout error while connecting to the server.

The problem might also be due to my code design. Here's my Restful client class

public class Restful
{
    public string auth = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("MyUserName:MyPassword"));
    
    public string POST(string parameters)
    {
         var request = (HttpWebRequest)WebRequest.Create("http://myserverdomain.com/api/webservice/someMethod");

        byte[] byteArray = Encoding.UTF8.GetBytes(parameters);

        request.Method = WebRequestMethods.Http.Post;
        request.Headers["Authorization"] = this.auth;
        request.ContentLength = byteArray.Length;
        request.ContentType = "application/x-www-form-urlencoded";

        Stream postStream = null;

        try
        {
            // ERROR IS IN THIS LINE
            postStream = request.GetRequestStream();
        }
        catch (WebException ex)
        {
            // I'm kind of creating an hack here..which isn't good..
            if (ex.Status.ToString() == "ConnectFailure")
            {
                System.Threading.Thread.Sleep(1000);
                this.POST(parameters);
            }
        }

        if (postStream == null)
            return string.Empty;

        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            var responseValue = string.Empty;

            if (response.StatusCode != HttpStatusCode.OK)
                return responseValue;

            using (var responseStream = response.GetResponseStream())
                if (responseStream != null)
                    using (var reader = new StreamReader(responseStream))
                        responseValue = reader.ReadToEnd();

            return responseValue;
        }
    }
}

I'm receiving the error (after a few successfully send items):

Unable to connect to remote server

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond MyServerIpAddress

Sometimes I send thousands of items to the server, sometimes I only send 2 or 4. So I call this POST function within a loop.

try
{
    foreach (app.views.Documents doc in DocumentsView)
    {
        var parameters = "key=" + doc.key;
            parameters += "&param1=" + doc.param1 + "&param2=" + doc.param2;
        
        /*
         * Validates the response of Restful service.
         * The response is always in JSON format.
         */
        if (response(Restful.POST(parameters)) == false)
        {
            MessageBox.Show("Error while sending the ID: " + doc.id.ToString());
            break;
        };
    }
}
catch (Exception ex)
{
    MessageBox.Show("Error while sending documents: " + ex.Message);
}
1
  • I would also suggest to give some thought as to why your service is taking so long to respond. Increasing time out is not a resolution. The problem could be some place else which we might often oversee. Commented Mar 14, 2016 at 17:36

2 Answers 2

6

You can change the default timeout of your HttpWebRequest to be some thing larger than the default, for example:

request.Timeout = 120000;

I think the default is 100 seconds.

You can also check this adjusting-httpwebrequest-connection-timeout-in-c-sharp

Sign up to request clarification or add additional context in comments.

Comments

0

taking the failure at face value, it says that the remote machine didnt respond. Most likely causes

  • wrong name or ip address
  • windows firewall is on on the remote machine

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.