簡體   English   中英

無法發送此HttpWebRequest

[英]Can't send this HttpWebRequest

我已經制作了一個彈出框,其中顯示了來自以下示例的Google驗證碼: http://www.google.com/sorry/?continue=http://www.google.com/ajax/rd%3Fsky%3Deeuiphp%26ludocid%3D6083833001863340802%26rdu%3DCNzphIbM6rECFWkGtAodP3UAGQ%26sig%3DaY3%26q%3Dtandl%25C3%25A4kare%252Cg%25C3%25B6teborg%2BSweden : http://www.google.com/sorry/?continue=http://www.google.com/ajax/rd%3Fsky%3Deeuiphp%26ludocid%3D6083833001863340802%26rdu%3DCNzphIbM6rECFWkGtAodP3UAGQ%26sig%3DaY3%26q%3Dtandl%25C3%25A4kare%252Cg%25C3%25B6teborg%2BSweden continue http://www.google.com/sorry/?continue=http://www.google.com/ajax/rd%3Fsky%3Deeuiphp%26ludocid%3D6083833001863340802%26rdu%3DCNzphIbM6rECFWkGtAodP3UAGQ%26sig%3DaY3%26q%3Dtandl%25C3%25A4kare%252Cg%25C3%25B6teborg%2BSweden : http://www.google.com/sorry/?continue=http://www.google.com/ajax/rd%3Fsky%3Deeuiphp%26ludocid%3D6083833001863340802%26rdu%3DCNzphIbM6rECFWkGtAodP3UAGQ%26sig%3DaY3%26q%3Dtandl%25C3%25A4kare%252Cg%25C3%25B6teborg%2BSweden

然后,我在程序內的框中輸入驗證碼,然后將信息發送給下面的下一個函數,該函數應該將數據提交給Google。 我嘗試使用Fiddler進行跟蹤,但似乎無法弄清楚出了什么問題。 該請求僅返回另一個驗證碼圖像。

在Fiddler中,我可以看到我何時進入瀏覽器並提交GET類型的請求,但是我該如何提交數據呢? 我試圖將我的POST切換到GET等,但是總是得到相同的錯誤,只是另一個驗證碼圖像。

任何想法可能有什么問題嗎?

public string submitGoogleCaptcha(string html, string captchaCode)
    {
        CookieContainer cookieCont = new CookieContainer();

        HttpWebRequest _wReq;
        HttpWebResponse _wResp;
        System.IO.StreamReader _sr;
        System.Text.ASCIIEncoding _enc = new System.Text.ASCIIEncoding();

        string continueValue = Regex.Match(html, @"<input type=""hidden"" name=""continue"" value=""\s*(.+?)\s*"">", RegexOptions.IgnoreCase).Groups[1].Value;
        string idFromGoogle = Regex.Match(html, @"<input type=""hidden"" name=""id"" value=""\s*(.+?)\s*"">", RegexOptions.IgnoreCase).Groups[1].Value;

        string _html = "";
        string postData = "continue=" + continueValue + "&id=" + idFromGoogle + "&captcha=" + captchaCode + "&submit=Submit";

        try
        {
            byte[] _data = _enc.GetBytes(postData);
            _wReq = (HttpWebRequest)WebRequest.Create("http://www.google.com/sorry/Captcha?");


            _wReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            _wReq.Referer = "http://www.google.com/sorry/?continue=" + HttpUtility.UrlEncode(continueValue);
            _wReq.KeepAlive = true;


            _wReq.Method = "POST";
            _wReq.ContentType = "application/x-www-form-urlencoded";
            _wReq.ContentLength = _data.Length;
            _wReq.CookieContainer = cookieCont;
            _wReq.AllowAutoRedirect = true;
            _wReq.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            System.IO.Stream _outStream = _wReq.GetRequestStream();
            _outStream.Write(_data, 0, _data.Length);
            _outStream.Close();
            _wResp = (HttpWebResponse)_wReq.GetResponse();
            _sr = new System.IO.StreamReader(_wResp.GetResponseStream());
            _html = _sr.ReadToEnd();
            _sr.Close();
            _wResp.Close();
        }
        catch (Exception ee)
        {

        }


        return _html;
    }

當我嘗試發布的代碼時,會遇到異常。 您會知道,如果沒有使用catch(Exception ee){}自己隱藏所有異常。 您得到的異常是試圖告訴您服務器發回了503錯誤“服務器不可用”。

我建議您學習一些基本的異常處理原理。

另外,我在Fiddler中看到了請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM