0

我从stackoverflow的某个地方得到了这段代码。我无法理解。它用于登录 youtube。我是 C# 和 OOPS 的新手。所以任何人都可以向我解释一些事情:

1) 在第 3 行

 HttpWebRequest request = GetNewRequest("https://accounts.google.com/ServiceLoginAuth", cookies);

HttpWebRequest 是一个类,request 是一个对象。但是为什么在那之后不使用 New 关键字呢?这是什么“GetNewRequest”?它是一种方法吗?大多数时候,我们使用带有 New 关键字的类的构造函数。那么我可以得出结论,我可以在那里使用该类的任何方法吗?有没有必要使用new关键字和构造函数?

2) 在第 6 行

Dictionary<string, string> parameters = new Dictionary<string, string>

字典是类,参数是对象。但这是什么<string, string>?我从来没有见过参数可以传递给一个类。

3) 在第 7 行

{
        {"continue","https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F"},
        {"service","youtube"},{"uilel","3"},{"dsh","157212168103955870"},{"hl","en_US"},
        {"GALX","PTqcwpZb2aE"},{"pstMsg","1"},{"dnConn",""}, {"checkConnection","youtube%3A248%3A1"}, 
        {"checkedDomains","youtube"}, {"timeStmp",""}, {"secTok",""}, {"Email","username"}, {"Passwd","password"}, 
        {"signIn","Sign+in"}, {"PersistentCookie","yes"}, {"rmShown","1"}};

我看不懂这是什么?它不适合我在 C# 中阅读的任何语法。这是我以前从未见过的。

完整代码

public void Login()
{
    HttpWebRequest request = GetNewRequest("https://accounts.google.com/ServiceLoginAuth", cookies);
    request.Referer = "https://accounts.google.com/ServiceLogin?passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F&uilel=3&hl=en_US&service=youtube";
    request.Host = "accounts.google.com";
    Dictionary<string, string> parameters = new Dictionary<string, string>{
        {"continue","https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F"},
        {"service","youtube"},{"uilel","3"},{"dsh","157212168103955870"},{"hl","en_US"},
        {"GALX","PTqcwpZb2aE"},{"pstMsg","1"},{"dnConn",""}, {"checkConnection","youtube%3A248%3A1"}, 
        {"checkedDomains","youtube"}, {"timeStmp",""}, {"secTok",""}, {"Email","username"}, {"Passwd","password"}, 
        {"signIn","Sign+in"}, {"PersistentCookie","yes"}, {"rmShown","1"}};
    HttpWebResponse response = MakeRequest(request, cookies, parameters);
    response.Close();
}
4

2 回答 2

5
  1. GetNewRequest将是一个实际创建并返回HttpWebRequest. 因此,new在调用GetNewRequest. new仅当您在声明所在的同一行中调用类型的构造函数时才需要。
  2. 之后的<string, string>声明Dictionary指定了键和值类型,因为Dictionary<TKey, TValue>它是泛型类型
  3. 这是一种使用键/值对列表初始化字典的方法。这只是语法糖。请参阅对象初始化。它只是为了让您的生活更轻松/代码更简洁。该代码相当于更详细的代码:

_

Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("continue","https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F");
parameters.Add("service","youtube");
// etc.
于 2012-12-20T13:12:41.753 回答
0
  1. GetNewRequest是您发布的代码中未定义的方法。
  2. Dictionary<string, string>和符号用于泛型, <有关介绍,请参阅http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx>
  3. 是初始化字典值的语法,见http://msdn.microsoft.com/en-us/library/bb384062.aspx
于 2012-12-20T13:14:47.103 回答