views:

351

answers:

2

I am having difficulty in consuming the reCaptcha Web Service using C#/.Net 3.5. Although I think the problem is with consuming web services in general.

String validate = String.Format("http://api-verify.recaptcha.net/verify?privatekey={0}&remoteip={1}&challenge={2}&response={3}", PrivateKey, UserIP, Challenge, Response);
WebClient serviceRequest = new WebClient();
serviceRequest.Headers.Add("ContentType","application/x-www-form-urlencoded")

String response = serviceRequest.DownloadString(new Uri(validate ));

It keeps telling me that the error is: nverify-params-incorrect. Which means:

The parameters to /verify were incorrect, make sure you are passing all the required parameters.

But it's correct. I am using the private key, the IP address (locally) is 127.0.0.1, and the challenge and response seem fine. However the error keeps occurring.

I am pretty sure this is a issue with how I am requesting the service as this is the first time I have actually used webservices and .Net.

I also tried this as it ensures the data is posted:

    String queryString = String.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}",PrivateKey, UserIP, Challenge, Response);
    String Validate = "http://api-verify.recaptcha.net/verify" + queryString;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Validate));
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = Validate.Length;


    **HttpWebResponse captchaResponse = (HttpWebResponse)request.GetResponse();**
    String response;
    using (StreamReader reader = new StreamReader(captchaResponse.GetResponseStream()))
        response = reader.ReadToEnd();

Seems to stall at the point where I get response.

Any advice?

Thanks in advance

A: 

Haven't worked with the recaptcha service previously, but I have two troubleshooting recommendations:

  1. Use Fiddler or Firebug and watch what you're sending outbound. Verifying your parameters would help you with basic troubleshooting, i.e. invalid characters, etc.
  2. The Recaptcha Wiki has an entry about dealing with development on Vista. It doesn't have to be limited to Vista, though; if you're system can handle IPv6, then your browser could be communicating in that format as a default. It appears as if Recaptcha deals with IPv4. Having Fiddler/Firebug working would tell you about those other parameters that could be causing you grief.

This may not help solve your problem but it might provide you with better troubleshooting info.

jro
A: 

So got this working, for some reason I needed to write the request to a stream like so:

//Write data to request stream 
        using (Stream requestSteam = request.GetRequestStream())
            requestSteam.Write(byteData, 0, byteData.Length);

Could anyone explain why this works. I didn't think I would need to do this, don't completely understand what's happening behind the scenes..

Damien
One possibility is that your original code, i.e. "new Uri(validate)" was truncating the length of the request. (Which might explain the nverify-params-incorrect result.) Using Fiddler would indicate if that was occurring.Nonetheless, with this method, all data is pushed into the RequestStream, which can handle any chunking for large data blocks as needed.Without debug info on the original code, it's hard to say.
jro