为Google.Apis.YouTube.v3设置代理 [英] Set proxy for Google.Apis.YouTube.v3
问题描述
我有以下代码来调用
YouTubeService服务= new YouTubeService(新的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中:
< defaultProxy useDefaultCredentials =falseenabled =true>
< proxy usesystemdefault =Falseproxyaddress =http://192.111.111.102:8081/>
< / defaultProxy>
然而,这似乎不起作用,因为当调用时,我得到以下错误:
System.Net.Sockets.SocketException:由于目标机器主动拒绝它而无法建立连接216.58.213.74:443
有没有办法在代码中手动设置代理?
沿着以下路线:
WebProxy proxy = new WebProxy(192.111.111.102,8081);
proxy.Credentials = new NetworkCredential(AppSettings.Variables.ProxyUser,AppSettings.Variables.ProxyPassword,AppSettings.Variables.ProxyDomain);
//将其应用于此处的服务或请求对象
VideoListResponse
对象: 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请求= WebRequest.Create(api);
WebProxy代理=新的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());
$ b catch(Exception ex)
{
ErrorLog.LogError(ex,Video entity processing error:);
}
I have the following bit of code to make a call to the
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();
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: 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?
Something along the lines of:
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
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设置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!