(eg: base station positioning) Use HttpClient to simulate Http request and get the return result

Let's take a look at the code first:

/**
 * Query longitude (longitude) and latitude (latitude) by Lac and cell_id
 * @param lac LAC number
 * @param cell_id cell_id number
 * @return Map:key -- longitude: longitude
 * latitude: latitude
 * address: address
 * /
 public Map queryLongitudeAndLatitude (String lac , String cell_id) throws IOException{

    Map<String,String> map = new HashMap<String,String>();
// 生成一个httpclient对象,模拟http请求
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.cellid.cn/m/cidInfo.php?lac=" + lac + "&cell_id=" + cell_id + "&hex=false&flag=2262433451");
httpPost.setHeader("Accept:","*/*");
httpPost.setHeader("Accept-Encoding:","gzip, deflate");

                        httpPost.setHeader( "Connection:" , "keep-alive" ) ;
     httpPost.setHeader( "Origin:" , "http://www.cellid.cn" ) ;
     httpPost.setHeader( "Referer:" , "http: //www.cellid.cn/m/index.php" ) ; //Referer: Many websites may report illegal access if we visit directly. Generally,
     HttpResponse response = httpclient.execute(httpPost) ;
     InputStream fis = response.getEntity().getContent() ;
 //Write content to StringBuilder
 BufferedReader reader = new BufferedReader( new InputStreamReader(fis)) ;
StringBuilder sb = new     
            StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    fis.close() ;
 //Address is wrapped by <br> tag
 int a = sb.indexOf( "<br>" ) + "<br>" .length() ;
     int b = sb.lastIndexOf( "<br> " ) ;
 //The latitude and longitude are wrapped by coord and addressInfo
 int c = sb.indexOf( "coord=" ) + "coord=" .length() ;
     int d = sb.lastIndexOf( "&addressInfo" ) ;
 //Get the latitude and longitude
 String jw = sb.substring(c , d) ;
 String address = sb.substring(a , b) ;//Address
 String latitude = jw.substring(
                        
        0,jw.indexOf(",")); //纬度
    String longitude = jw.substring(jw.indexOf(",") + 1); //经度
map.put("address",address);
map.put("latitude",latitude);
map.put("longitude",longitude);
    return map;
}
            

Some explanation of what's in the code:

Because what I do is base station positioning related content, I visit the website I need.

httpPost.setHeader("xxxx");

The settings here should be set according to which website we want to visit, F12 developer mode. After grabbing the request information, you can set it according to the content of the request information.

The code goes all the way to fis.close(); this part is all obtained the entity in the returned response, and then the value can be obtained as required. I need the address and latitude and longitude here, so I get the value of this part.

看一下response.entity中有些什么,这样或许会更清楚我上面的代码是在做什么:

sb = {StringBuilder@1808}"基站:34860,62041<br>云南省西双版纳傣族自治州景洪市锦绣路<br><a href='../location.php?lac=34860&cellid=62041&coord=22.01436,100.752683&addressInfo=云南省西双版纳傣族自治州景洪市锦绣路' target='_blank'>(查看地图</a>)"


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324476773&siteId=291194637