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>