0

I have a really strange error which has been driving me mad for days.

I'm running the following code within my Visual Studio development environment and getting the error "An unexpected error occurred on a send". Compile the code and move it to a machine that does not have Visual Studio and it works fine

Environment is .Net 4.7.1 using Visual Studio 2015

public string Get(string resource)
{
   string url = BuildResourceUrl(resource);
   try
   {
            WebRequest req = WebRequest.Create(url);
            req.Method = "GET";

            req.ContentType = "application/json";
            req.Headers.Add("X-Shopify-Access-Token", _apiPassword);

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            WebResponse response = req.GetResponse();

            Stream dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);

            string responseFromServer = reader.ReadToEnd();

            reader.Close();
            dataStream.Close();
            response.Close();

            return responseFromServer;
   }
   catch (WebException ex)
   {
            string response;

            using (var reader = new StreamReader(ex.Response.GetResponseStream()))
            {
                response = reader.ReadToEnd();
            }
            throw ex;
   }

}

I have disabled all Firewalls and AntiVirus but this makes no difference.

Help on resolving this would be most appriciated

  • Where so you get this message? In `ex` or in `response` inside the catch block – Chetan Jul 10 '18 at 10:07
  • Try specifying the TLS options as in this answer: https://stackoverflow.com/a/43992910/246342 – Alex K. Jul 10 '18 at 10:09
  • The best way of debugging these issues is to use a sniffer like wireshark or fiddler. Compare the headers in the good results against the bad results. Most likely in this case the browser installed in the PCs are different. You may need to add a header for the contents type. You also may have to remove cookies manually in the webbrowser. There may be a bad cookie in the development system that is preventing the code from running. – jdweng Jul 10 '18 at 10:19
  • I get this error on the ex, I have tried changing the SecurityProtocal to ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; but this has not worked either – Daniel Gradwell Jul 10 '18 at 12:54

1 Answers1

0

After much digging and frustration, I have found the solution. Even with using .Net 4.7.1 I still have to use the following code

public string Get(string resource)
    {
        string url = BuildResourceUrl(resource);
        try
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            WebRequest req = WebRequest.Create(url);
            req.Method = "GET";

            req.ContentType = "application/json";
            req.Headers.Add("X-Shopify-Access-Token", _apiPassword);

            WebResponse response = req.GetResponse();

            Stream dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);

            string responseFromServer = reader.ReadToEnd();

            reader.Close();
            dataStream.Close();
            response.Close();

            return responseFromServer;
        }
        catch (WebException ex)
        {
            string response;

            using (var reader = new StreamReader(ex.Response.GetResponseStream()))
            {
                response = reader.ReadToEnd();
            }
            throw ex;
        }
    }

This seems to be a Shopify specific issue as I'm not having any problems placing API calls to other providers