2

I'm trying to get a JSON response from a service in my C# code. I generate the URL, and pass it to the webClient.DownloadString method, where it comes back with a 400 error. I can copy the URL into a browser and get a correct response.

What might I be missing?

Here's the code:

using (var webClient = new WebClient())
{
    try
    {
        var requestUrl = GetRequestUrl(email);
        // Error here
        var jsonResponse = webClient.DownloadString(requestUrl);
        //...
    }
    catch (Exception exception)
    {
        // ...
    }
}

Here's a sample of the URL as it's generated from GetRequestUrl(email) which returns a string. (Certain values redacted)

http://api.someservice.org/api.php?usr=xxxxyyyy&pwd=zzz2345mmm22&check=none@nowhere.com

And here's what I get with an actual URL from a browser.

{"authentication_status":1,"limit_status":0,"limit_desc":"Not Limited","verify_status":1,"verify_status_desc":"MX record about nowhere.com exists.Connection succeeded to nowhere-com.mail.protection.outlook.com SMTP.220 BN3NAM01FT045.mail.protection.outlook.com Microsoft ESMTP MAIL Service ready at Mon, 29 Feb 2016 14:52:44 +0000\n> HELO verify-email.org250 BN3NAM01FT045.mail.protection.outlook.com Hello [verify-email.org]\n> MAIL FROM: <check@verify-email.org>=250 2.1.0 Sender OK\n> RCPT TO: <none@nowhere.com>=250 2.1.5 Recipient OK\n"}

UPDATE: Here's what GetRequestUrl does:

protected string GetRequestUrl(string email)
{
    if (string.IsNullOrEmpty(email))
        throw new ArgumentNullException("email");

    var requestUrl = string.Format("http://api.someservice.org/api.php?usr={0}&pwd={1}&check={2}", ApiUsername, ApiPassword, Uri.EscapeUriString(email));

    return requestUrl;
}

UPDATE: Here is the exception message, InnerException is NULL.

The remote server returned an error: (400) Bad Request.

UPDATE: Caught the WebException, and got the full ResponseStream. I don't think the call is actually going out, Fiddler doesn't catch a call to the address. Here's the exception.Response:

<h2>Bad Request - Invalid Hostname</h2>
<p>HTTP Error 400. The request hostname is invalid.</p>
M Kenyon II
  • 4,136
  • 4
  • 46
  • 94
  • Did you try to examine the call with tools such as fiddler? – shay__ Feb 29 '16 at 15:18
  • Since 400 is Bad Request, is it possible that you have characters in the URL that need to be escaped? What does `GetRequestUrl` return? – Jim Rhodes Feb 29 '16 at 15:22
  • Also, you can try to use an HttpWebRequest to see what's the error as you will be able to get the response content. – Gusman Feb 29 '16 at 15:22
  • I think the problem is with this argument `&check=none@nowhere.com`. Try Encoding the url before passing it to webclient. You can use e.g. `System.Web.Mvc.HtmlHelper.Encode(url)` method. – vendettamit Feb 29 '16 at 16:16
  • I've pulled the code into LinqPad, hard coding the URL string. It works correctly. Fiddler picks up the Request. However, running via debug from visual Studio, Fiddler does not see the call. I tried setting the header: `webClient.Headers["Content-Type"] = "application/json;charset=UTF-8";`. It still does not work. – M Kenyon II Feb 29 '16 at 19:33
  • Have you contacted whoever it is that created or owns the API? 400 Bad request could mean a lot of different things; it could be that the API is looking for a `User-Agent:` header and accepts specific user agents. One might do something like this in order to prevent abuse by "bots" (just throwing out one possibility). – CodingGorilla Feb 29 '16 at 19:52
  • The actual host is: "api.verify-email.org" – M Kenyon II Feb 29 '16 at 19:56
  • If I use LinqPad, and reference this code, it works. When I debug from Chrome it doesn't work. It almost seems like 2 different DLLs are being used. Does that ring any bells? – M Kenyon II Feb 29 '16 at 21:35
  • Check the WebException message type instead of the default Exception type as demonstrated here... https://stackoverflow.com/a/55165046/1165173 – nimblebit Oct 27 '22 at 15:56

0 Answers0