联系客服: iP138.com客服 iP138客服 |

iP查询接口
  • API接口QQ交流群:177096428   iP138 api接口
  • 数据修正QQ处理群:94181690    iP138数据修正

iP查询接口支持HTTPS(赠送1000次)

简介:获取iP地址对应的省市区以及运营商名称

已连接应用数:25106

国内:网宿cdn; 国际:亚洲中国香港、韩国首尔、日本东京、新加坡、欧洲德国法兰克福、北美洲美国硅谷节点

接口地址

http协议:http://api.ip138.com/ipv4/
https协议:https://api.ip138.com/ipv4/

* API接口可能会因为各种网络原因和攻击都可能产生阻断,请开发时做好冗余和异常处理

* 当HTTP请求返回的状态码非200时,请做异常处理,比如 202 状态码造成的原因可能是无效Token、余额不足、格式错误

Android调用iP查询接口示例:

                                    package com.lmqkk.test;

                                    import android.os.AsyncTask;
                                    import android.os.Build;
                                    import android.os.Bundle;
                                    import android.support.v7.app.AppCompatActivity;
                                    import android.util.Log;

                                    import java.io.ByteArrayOutputStream;
                                    import java.io.DataOutputStream;
                                    import java.io.IOException;
                                    import java.io.InputStream;
                                    import java.io.UnsupportedEncodingException;
                                    import java.net.HttpURLConnection;
                                    import java.net.MalformedURLException;
                                    import java.net.SocketTimeoutException;
                                    import java.net.URL;
                                    import java.net.URLEncoder;
                                    import java.util.HashMap;
                                    import java.util.Map;

                                    public class MainActivity extends AppCompatActivity {
                                        @Override
                                        protected void onCreate(Bundle savedInstanceState) {
                                            super.onCreate(savedInstanceState);
                                            setContentView(R.layout.activity_main);

                                            QueryTask task = new QueryTask();
                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                                task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"8.8.8.8","txt","find","859476648b3de65d76804ff906dd1a1c6a");
                                            } else {
                                                task.execute("8.8.8.8","txt","find","859476648b3de65d76804ff906dd1a1c6a");
                                            }
                                        }

                                        class QueryTask extends AsyncTask<String,Void,String>
                                        {
                                            @Override
                                            protected String doInBackground(String... strings) {
                                                String strUrl = "https://api.ip138.com/ipv4/" ;

                                                Map<String,String> params = new HashMap<String,String>();
                                                params.put("ip",strings[0]);       //可为空
                                                params.put("datatype",strings[1]);
                                                params.put("callback",strings[2]); //可为空
                                                String result = null;

                                                try {
                                                    URL url = new URL(strUrl);
                                                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                                                    //头信息
                                                    HashMap<String, String> header = new HashMap<String,String>();
                                                    header.put("token", strings[3]);
                                                    for (String headerName : header.keySet()) {
                                                        connection.addRequestProperty(headerName, header.get(headerName));
                                                    }

                                                    //设置超时时间
                                                    connection.setConnectTimeout(5*1000);
                                                    //设置允许输入
                                                    connection.setDoInput(true);
                                                    //设置读取超时:
                                                    connection.setReadTimeout(5*1000);
                                                    connection.setRequestMethod("GET");
                                                    //参数
                                                    byte[] body = encodeParameters(params,"UTF-8");
                                                    if (body != null)
                                                    {
                                                        connection.setDoOutput(true);
                                                        connection.addRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=" + "UTF-8");
                                                        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                                                        out.write(body);
                                                        out.close();
                                                    }
                                                    int responseCode = connection.getResponseCode();
                                                    if (responseCode == -1) {
                                                        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
                                                    }
                                                    if (responseCode != 200)
                                                    {
                                                        return responseCode + "";
                                                    }

                                                    InputStream inputStream = null;
                                                    try {
                                                        inputStream = connection.getInputStream();
                                                    } catch (IOException ioe) {
                                                        inputStream = connection.getErrorStream();
                                                    }

                                                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                                                    byte[] data = new byte[1024];
                                                    int len = 0;
                                                    if (inputStream != null) {
                                                        try {
                                                            while ((len = inputStream.read(data)) != -1) {
                                                                outputStream.write(data, 0, len);
                                                            }
                                                            result = new String(outputStream.toByteArray(), "UTF-8");
                                                        }
                                                        catch (IOException e) {
                                                            e.printStackTrace();
                                                        }
                                                    }

                                                    return result;
                                                }catch(SocketTimeoutException e)
                                                {

                                                }catch (MalformedURLException e) {

                                                } catch (IOException e) {

                                                }
                                                return null;
                                            }

                                            @Override
                                            protected void onPostExecute(String s) {
                                                super.onPostExecute(s);
                                                Log.i("Query", "onPostExecute: "+s);
                                            }

                                            byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
                                                if (params == null || params.size() == 0) return null;

                                                StringBuilder encodedParams = new StringBuilder();
                                                try {
                                                    for (Map.Entry<String, String> entry : params.entrySet()) {
                                                        encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
                                                        encodedParams.append('=');
                                                        encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
                                                        encodedParams.append('&');
                                                    }
                                                    return encodedParams.toString().getBytes(paramsEncoding);
                                                } catch (UnsupportedEncodingException uee) {
                                                    throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
                                                }
                                            }
                                        }
                                    }