HttpWebRequest.GetResponse() keeps getting timed out

前端 未结 5 2088
悲哀的现实
悲哀的现实 2020-12-17 09:16

i wrote a simple C# function to retrieve trade history from MtGox with following API call:

https://data.mtgox.com/api/1/BTCUSD/trades?since=
         


        
5条回答
  •  Happy的楠姐
    2020-12-17 09:46

    I just had similar troubles calling a REST Service on a LINUX Server thru ssl. After trying many different configuration scenarios I found out that I had to send a UserAgent in the http head.

    Here is my final method for calling the REST API.

            private static string RunWebRequest(string url, string json)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
            // Header
            request.ContentType = "application/json";
            request.Method = "POST";
            request.AllowAutoRedirect = false;
            request.KeepAlive = false;
            request.Timeout = 30000;
            request.ReadWriteTimeout = 30000;
            request.UserAgent = "test.net";
            request.Accept = "application/json";
            request.ProtocolVersion = HttpVersion.Version11;
            request.Headers.Add("Accept-Language","de_DE");
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            byte[] bytes = Encoding.UTF8.GetBytes(json);
            request.ContentLength = bytes.Length;
            using (var writer = request.GetRequestStream())
            {
                writer.Write(bytes, 0, bytes.Length);
                writer.Flush();
                writer.Close();
            }
    
            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var jsonReturn = streamReader.ReadToEnd();
                return jsonReturn;
            }
        }
    

提交回复
热议问题