简体   繁体   中英

HttpWebRequest No Response when calling an external website API

I am trying to use an API that helps detect bad IP , with C# code.

Here is the documentation.

How to call the API?

API requests are sent in a specific form. The API key is sent directly to the URL, as is the IP address from which you want to retrieve the information. This is the form of the URL called.

https://api.ipwarner.com/API-KEY/IP

According to this, I wrote a function:

private static string Get(string uri)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException());
    return reader.ReadToEnd();
}

And called it with:

string myresult = Get("https://api.ipwarner.com/myapikey/myip");

However, it got stuck at HttpWebResponse . There was no response at all.

(I confirm my API key is available and the input IP is right)

How's that wrong?

Please set time out and Try. Now, You will get the error message. Work on the error.

private static string Get(string uri)
 {
   string returnStr  = trsing.Empty;
  try
   {    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Timeout=10;
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream ?? throw new 
    InvalidOperationException());
    returnStr  = reader.ReadToEnd();
 }
 catch( Exception ex)
 {
  Debug.Writeline( ex.ToString());
 }

 return returnStr  ;
}

It's not your problem i have checked it right now and the api is down

you can also check

https://www.isitdownrightnow.com/api.ipwarner.com.html

i signed up the site and tested it my self the api site is down

contact there support

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM