400 error with HttpClient for a link with an anchor

前端 未结 4 1530
死守一世寂寞
死守一世寂寞 2021-01-17 03:15

Here is my code:

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
         


        
4条回答
  •  鱼传尺愫
    2021-01-17 03:39

    Your usage of the anchor in the url is incorrect. When we perform a "Get", we need to get the entire resource (page). The anchor is just a tag marking a location, normally your browser will scroll to the position of the anchor once the page is loaded. It does not make sense to "Get" the page at a specific anchor - the entire page must be fetched.

    It is possible your inconsistent results are because some web servers are ignoring the anchor component, and others are correcting your error.

    The solution is just to remove the #anchor portion of the url before running your code.

提交回复
热议问题