如何从其他网站绘制验证码图像

时间:2015-01-27 13:12:50

标签: c# asp.net .net streamreader

我有一个重要的项目。我必须从运行其他网站验证码中动态绘制验证码图像。在这里,我必须向其他登录页面发出一个Web请求。并绘制验证码与显示其他网站相同。在这里,我必须将此验证码图像src设置为我的asp图像控件,用户可以在登录成功后输入它。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var request = (HttpWebRequest)WebRequest.Create("https://incometaxindiaefiling.gov.in/e-Filing/UserLogin/LoginHome.html");

            //var postData = "User ID=BEPPG5845B";
            //postData += "&password=shalsoft$7008";
            //postData += "&dob=21/09/1991";
            //var data = Encoding.ASCII.GetBytes(postData);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            //request.ContentLength = data.Length;

            //using (var stream = request.GetRequestStream())
            //{
                //stream.Write(data, 0, data.Length);
            //}

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = (dynamic)null ;

            using (responseString = new StreamReader(response.GetResponseStream()).ReadToEnd())
            {
                responseString = Regex.Replace(responseString, @"\s+", string.Empty);
                responseString = ((String)responseString).Replace("\"", "'");
                if (responseString != null)
                {

                    int captha = ((String)responseString).IndexOf("<img id='captchaImg'");
                    if (captha != -1)
                    {
                       responseString = ((String)responseString).Remove(0, captha + 19);
                       int src = ((String)responseString).IndexOf("src");
                       if (src != -1)
                       {
                           responseString = ((String)responseString).Remove(0, src + 5); 
                           int dot = ((String)responseString).IndexOf(".png");
                           if(dot != -1)
                           {
                               Image1.ImageUrl = ((String)responseString).Substring(0, dot + 4);
                           }
                       }
                    }
                }
            }
        }
    }
}

这里给我这样的错误:

Server Error in '/PAN' Application.
Cannot implicitly convert type 'string' to 'System.IDisposable'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'string' to 'System.IDisposable'

Source Error:


Line 35:             var responseString = (dynamic)null ;
Line 36: 
Line 37:             using (responseString = new StreamReader(response.GetResponseStream()).ReadToEnd())
Line 38:             {
Line 39:                 responseString = Regex.Replace(responseString, @"\s+", string.Empty);


Source File: c:\Users\SHALIN\Documents\Visual Studio 2010\WebSites\PAN\Default.aspx.cs    Line: 37

Stack Trace:


[RuntimeBinderException: Cannot implicitly convert type 'string' to 'System.IDisposable']
   CallSite.Target(Closure , CallSite , Object ) +139
   System.Dynamic.UpdateDelegates.UpdateAndExecute1(CallSite site, T0 arg0) +438
   _Default.Page_Load(Object sender, EventArgs e) in c:\Users\SHALIN\Documents\Visual Studio 2010\WebSites\PAN\Default.aspx.cs:37
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 

1 个答案:

答案 0 :(得分:1)

包含的源代码和错误是太不同的事情。

但是,错误是一个强制转换错误,将流转换为字符串的正确方法是这样的:

string responseString = null;
using(StreamReader reader = new StreamReader(response.GetResponseStream())
{
    responseString = reader.ReadToEnd();
    // ...
}