2012-08-16 78 views
0

我已經顯示從例如谷歌驗證碼的彈出框: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無法發送此HttpWebRequest的

我然後在驗證碼輸入到我的計劃之內框和發送信息發給我的下一個函數下面,它應該提交數據給谷歌。我試圖用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; 
    } 

回答

0

當我嘗試你發佈的代碼時,你會得到一個異常。你會知道,如果你沒有隱藏自己的所有例外catch(Exception ee){}。你得到的例外是試圖告訴你服務器發回503錯誤「服務器不可用」。

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

此外,我看到在提琴手的請求。

+0

這個錯誤是否表明我提交給錯誤的URL? – gwenda 2012-08-16 13:26:45