Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Google Sheets API v4,SocketException:现有连接被远程主机强制关闭_C#_Http_Google Sheets - Fatal编程技术网

C# Google Sheets API v4,SocketException:现有连接被远程主机强制关闭

C# Google Sheets API v4,SocketException:现有连接被远程主机强制关闭,c#,http,google-sheets,C#,Http,Google Sheets,我经历了许多解决方案。我在这里也尝试了一种解决方案,但它对我不起作用。因此,请不要将此标记为重复。我正在使用Google Sheets API v4,但有以下例外情况: System.AggregateException: 'One or more errors occurred.' HttpRequestException: An error occurred while sending the request. WebException: The underlying connectio

我经历了许多解决方案。我在这里也尝试了一种解决方案,但它对我不起作用。因此,请不要将此标记为重复。我正在使用Google Sheets API v4,但有以下例外情况:

System.AggregateException: 'One or more errors occurred.'
HttpRequestException: An error occurred while sending the request.

WebException: The underlying connection was closed: An unexpected error occurred on a send.

IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

SocketException: An existing connection was forcibly closed by the remote host
内部异常:

System.AggregateException: 'One or more errors occurred.'
HttpRequestException: An error occurred while sending the request.

WebException: The underlying connection was closed: An unexpected error occurred on a send.

IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

SocketException: An existing connection was forcibly closed by the remote host
当我尝试获取HttpResponseMessage的结果时。以下是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Http;
using System.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Security;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApp3
{
    static class Program
    {
        static string _apiKey;
        static string _sheetID;

        static string _baseUrl;
        static HttpClient _client;

        static Program()
        {
            _apiKey = ConfigurationManager.AppSettings["apiKey"];
            _sheetID = ConfigurationManager.AppSettings["sheetID"];

            _client = new HttpClient();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;


            _baseUrl = "https://sheets.googleapis.com/v4/spreadsheets/{0}/values/{1}!{2}";

        }

        static void Main(string[] args)
        {
            using(HttpRequestMessage __request = new HttpRequestMessage())
            {
                __request.Method = HttpMethod.Post;
                __request.RequestUri = new Uri(string.Format(_baseUrl, _sheetID, "Sheet1", "A1:D3"));

                Dictionary<string, object> __parameters = new Dictionary<string, object>();
                __parameters.Add("key", _apiKey);

                __request.Content = new StringContent(__parameters.ToJson(), Encoding.UTF8, "application/json");
                

                using (HttpResponseMessage ___response = _client.SendAsync(__request).Result) // Error Here
                {
                    
                }
            }
            Console.ReadKey();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.IO;
Net系统;
使用系统文本;
使用System.Net.Http;
使用系统配置;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用System.Net.Security;
使用System.Net.Http.Header;
使用System.Security.Cryptography.X509证书;
名称空间控制台AP3
{
静态类程序
{
静态字符串_apiKey;
静态字符串_sheetID;
静态字符串_baseUrl;
静态HttpClient\u客户端;
静态程序()
{
_apiKey=ConfigurationManager.AppSettings[“apiKey”];
_sheetID=ConfigurationManager.AppSettings[“sheetID”];
_client=新的HttpClient();
_client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
_baseUrl=”https://sheets.googleapis.com/v4/spreadsheets/{0}/values/{1}!{2}”;
}
静态void Main(字符串[]参数)
{
使用(HttpRequestMessage _request=new HttpRequestMessage())
{
__request.Method=HttpMethod.Post;
__request.RequestUri=新的Uri(string.Format(_baseUrl,_sheetID,“Sheet1”,“A1:D3”);
字典_参数=新字典();
__参数。添加(“键”、\u apiKey);
__request.Content=newstringcontent(_parameters.ToJson(),Encoding.UTF8,“application/json”);
使用(HttpResponseMessage __; response=\u client.SendAsync(_request.Result)//此处出错
{
}
}
Console.ReadKey();
}
}
}

请求中“:”字符的URL编码可能有问题

使用
System.Web.HttpUtility.UrlEncode()
确保图纸ID、图纸名称或单元格位置中的任何字符编码正确

在您的代码中,这一行

__request.RequestUri = new Uri(string.Format(_baseUrl, HttpUtility.UrlEncode(_sheetID), "Sheet1", HttpUtility.UrlEncode("A1:D3")));

请求中“:”字符的URL编码可能有问题

使用
System.Web.HttpUtility.UrlEncode()
确保图纸ID、图纸名称或单元格位置中的任何字符编码正确

在您的代码中,这一行

__request.RequestUri = new Uri(string.Format(_baseUrl, HttpUtility.UrlEncode(_sheetID), "Sheet1", HttpUtility.UrlEncode("A1:D3")));

它帮助anw,但它仍然抛出相同的异常它帮助anw,但它仍然抛出相同的异常。。