代码问题

时间:2012-12-03 15:35:27

标签: c#

try
{

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.site.ru/group/" + gr);
    request.AllowAutoRedirect = false;
    request.Method = "GET";
    request.CookieContainer = cookies;
    request.UserAgent = "Opera/9.80 (Windows 7; U; en) Presto/2.9.168 Version/11.50";
    request.ContentType = "application/x-www-form-urlencoded";
    HttpWebResponse response_headers = (HttpWebResponse)request.GetResponse();
    System.IO.Stream stream = response_headers.GetResponseStream();
    System.IO.StreamReader sr = new System.IO.StreamReader(stream);
    string response = sr.ReadToEnd();
    sr.Close();
    /*if (response_headers.Headers["Location"].Contains("alted"))
    {
        log("[-] GROUP is " + gr + " closed\r\n");
        return -2;
    }*/
    string gash = Regex.Match(response, @"gwtHash:""(?<id>[^""]+)""").Groups["id"].Value;
    string grpId = Regex.Match(response, @"state:""st.cmd=altGroupMain&amp;st.groupId=(?<id>[^""]+)""").Groups["id"].Value;
}
catch { log("[?] Can't parse ash and grpId\r\n"); return -1; }

当const {}被评论时,此代码可以成功运行。但是当我删除评论并运行它时,我会收到[?] Can't parse ash and grpId 为什么呢?= \

2 个答案:

答案 0 :(得分:3)

response_headers.Headers [“Location”] 必须为null,因此会引发异常。插入验证:

if (response_headers.Headers["Location"] != null && response_headers.Headers["Location"].Contains("alted"))
    {
        log("[-] GROUP is " + gr + " closed\r\n");
        return -2;
    }

答案 1 :(得分:1)

您收到该消息是因为您抓住所有代码的所有内容:

catch { log("[?] Can't parse ash and grpId\r\n"); return -1; }

这是一种不好的做法。删除try / catch块并让原始异常显示出来。这样就可以看出真正的问题是什么。