Set proxy for Google.Apis.YouTube.v3(为 Google.Apis.YouTube.v3 设置代理)
问题描述
我有以下代码可以调用
YouTubeService service = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = AppSettings.Variables.YouTube_APIKey,
ApplicationName = AppSettings.Variables.YouTube_AppName
});
Google.Apis.YouTube.v3.VideosResource.ListRequest request = service.Videos.List("snippet,statistics");
request.Id = string.Join(",", videoIDs);
VideoListResponse response = request.Execute();
这一切正常,但是当我们将其部署到我们的实时服务器时,它需要通过代理,因此我们将以下内容放入 web.config:
This all works but when we deploy it to our live server, it needs to get through a proxy so we put the following into the web.config:
<defaultProxy useDefaultCredentials="false" enabled="true">
<proxy usesystemdefault="False" proxyaddress="http://192.111.111.102:8081" />
</defaultProxy>
但是,这似乎不起作用,因为在拨打电话时,我收到以下错误:
However, this doesn't seem to be working as when the call is made, I get the following error:
System.Net.Sockets.SocketException:无法建立连接,因为目标机器主动拒绝了它 216.58.213.74:443
System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 216.58.213.74:443
有没有办法在代码中手动设置代理?
Is there a way to manually set the proxy in code?
类似的东西:
WebProxy proxy = new WebProxy("192.111.111.102", 8081);
proxy.Credentials = new NetworkCredential(AppSettings.Variables.ProxyUser, AppSettings.Variables.ProxyPassword, AppSettings.Variables.ProxyDomain);
// apply this to the service or request object here
推荐答案
为了解决这个问题,我必须向 url 发出 webrequest 并将结果映射回 VideoListResponse
对象:
To get around this I had to make a webrequest to the url and map the result back to the VideoListResponse
object:
try
{
Uri api = new Uri(string.Format("https://www.googleapis.com/youtube/v3/videos?id={0}&key={1}&part=snippet,statistics", videoIds, AppSettings.Variables.YouTube_APIKey));
WebRequest request = WebRequest.Create(api);
WebProxy proxy = new WebProxy(AppSettings.Variables.ProxyAddress, AppSettings.Variables.ProxyPort);
proxy.Credentials = new NetworkCredential(AppSettings.Variables.ProxyUsername, AppSettings.Variables.ProxyPassword, AppSettings.Variables.ProxyDomain);
request.Proxy = proxy;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
return JsonConvert.DeserializeObject<VideoListResponse>(streamReader.ReadToEnd());
}
}
}
catch (Exception ex)
{
ErrorLog.LogError(ex, "Video entity processing error: ");
}
这篇关于为 Google.Apis.YouTube.v3 设置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 Google.Apis.YouTube.v3 设置代理
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01