Validate Recaptcha : Captcha « Network « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Network » CaptchaScreenshots 
Validate Recaptcha
      

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Web.Mvc;

namespace WeBlog.Core {
  public static class RecaptchaUtils {
    public static JsonResult ValidateRecaptchastring privateKey, string remoteip, string challenge, string response) {
      string postData = String.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}",
                            privateKey, remoteip,
                            challenge, response);
      JsonResult result = new JsonResult();
      byte[] postDataBuffer = System.Text.Encoding.ASCII.GetBytes(postData);

      Uri serviceUri = new Uri("http://api-verify.recaptcha.net/verify", UriKind.Absolute);
      try {
        HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = postDataBuffer.Length;
        webRequest.Method = "POST";
        
        //incase you are using a proxy server
        IWebProxy proxy = WebRequest.GetSystemWebProxy();
        proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
        webRequest.Proxy = proxy;

        Stream requestStream = webRequest.GetRequestStream();
        requestStream.Write(postDataBuffer, 0, postDataBuffer.Length);

        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
        string jsonResponse = string.Empty;

        using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
          jsonResponse = sr.ReadToEnd();

        string[] tokens = jsonResponse.Split(new char[] { '\n' });
        if (tokens.Length == 2) {
          Boolean success = tokens[0].Equals("true", StringComparison.CurrentCulture);
          result.Data = new Success = success, Message = tokens[1] };
        }
        else {
          result.Data = new Success = false, Message = "Invalid response returned" };
        }
      }
      catch (Exception ex) {
        result.Data = new Success = false, Message = ex.Message };
      }

      return result;
    }
  }
}

   
    
    
    
    
    
  
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.