如何判断我的服务器端应用程序使用的引用者地址

时间:2015-06-25 16:51:12

标签: c# youtube ip

这似乎是一个简单的问题。我正在使用用C#编写的服务器端应用程序从Windows Server 2008服务器执行到youtube的简单api密钥连接。当我在没有ip referer限制的情况下运行它时它工作正常。所以我设置了ip referer限制。我在我的服务器上运行ipconfig并使用列出的主IP地址,我得到了一个ipreferer阻止错误。我也尝试了列出的其他IP地址。我已经包含了我的ipconfig的截图和笔记。

所以我知道程序正在向youtube传递一些引用ip,而且它不是正确的。我该如何判断它传递的是什么?

A screenshot of my ipconfig

代码如下:

/*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

namespace Google.Apis.YouTube.Samples
{
    /// <summary>
    /// YouTube Data API v3 sample: search by keyword.
    /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
    /// See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
    ///
    /// Set ApiKey to the API key value from the APIs & auth > Registered apps tab of
    ///   https://cloud.google.com/console
    /// Please ensure that you have enabled the YouTube Data API for your project.
    /// </summary>
    internal class Search
    {
        List<string> HTMLText = new List<string>();
        Boolean openDiv = true;
        string mainVideoTarget = "main_video";
        string menuDiv = "video_menu_images";
        string thumbnailDiv = "thumb";
        [STAThread]
        static void Main(string[] args)
        {

            try
            {
                new Search().Run().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    Console.WriteLine("Error: " + e.Message);

                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        private async Task Run()
        {
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = "API_KEY_HERE",
                ApplicationName = this.GetType().ToString()
            });

            var channelsListRequest = youtubeService.Channels.List("contentDetails");
            channelsListRequest.Id = "CHANNEL_ID_HERE";

            // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
            var channelsListResponse = await channelsListRequest.ExecuteAsync();


       //a bunch of stuff down here to read the data from youtube 
       //write it into an html file which I left out because it's irrelevant
       }
    }
}

2 个答案:

答案 0 :(得分:0)

ipconfig is returning the internal IP address. Try a site like http://whatismyip.com and use the IP address that site returns as the Referer address.

答案 1 :(得分:0)

感谢您的帮助和建议,特别指出ipconfig中的ip不是路由器传递的内容。我知道whatsmyip但是我不是100%确定是否与控制台应用程序将使用的ip相同。事实证明它是。我能够通过运行这个简单的小脚本找到传递的ip:

using System;
using System.IO;
using System.Net;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest htwr = (HttpWebRequest)WebRequest.Create("[any url that can get an http connection]");
            WebResponse response = htwr.GetResponse();
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            Console.WriteLine(responseFromServer);
            reader.Close();
            response.Close();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

        }
    }
}

返回的其中一个变量是REMOTE_ADDR,这正是我要找的。