在java中,如何克服HTTP get 404错误?

在java中,如何克服HTTP get 404错误?,java,apache,http,google-places-api,Java,Apache,Http,Google Places Api,我正在使用,并试图使一个新的,但有问题 下面是一个基本片段来说明这个问题: //Web API related String apiKey = "API_KEY"; //search params String location = "51.527277,-0.128625";//lat,lon int rad = 500; String types = "food"; String name = "pret"; String getURL = "/maps

我正在使用,并试图使一个新的,但有问题

下面是一个基本片段来说明这个问题:

  //Web API related
  String apiKey = "API_KEY"; 
  //search params
  String location = "51.527277,-0.128625";//lat,lon
  int rad = 500;
  String types = "food";
  String name  = "pret";

  String getURL = "/maps/api/place/search/json?location="+location+"&radius="+rad+"&types="+types+"&name="+name+"&sensor=false&key="+apiKey;
  HttpHost host = new HttpHost("maps.googleapis.com",443,"https");
  HttpGet get = new HttpGet(host.toURI() + getURL);
  System.out.println("using getRequestLine(): " + get.getRequestLine());
  System.out.println("using getURI(): " + get.getURI().toString());

  DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
  try {
      HttpResponse response = httpClient.execute(get);
      System.out.println("response: " + response.getStatusLine().toString());
  } catch (Exception e) {
      System.err.println("HttpClient: An error occurred- ");
      e.printStackTrace();
  }   
我得到的这个输出看起来有点像这样(当然API_键除外):

这一切都有点令人费解,因为:

  • 我对HTTP REST调用没有太多经验
  • 如果我在中尝试getRequestLine()url,我会得到一个状态200,数据如下所示:

    { “html_属性”:[], “结果”:[], “状态”:“请求被拒绝” }

  • 但是如果我使用getURI()版本,它就可以正常工作

    我不确定问题是附加的“HTTP/1.1”还是其他什么。
    这是从Java进行Google Places API查询的正确方法吗?

    这样手动构建URL可能会导致您的代码无法正确转义API密钥,而且如果主机是HTTP客户端上的HTTPS,您也不需要添加433,下面是一些有效的代码:

    import java.net.URI;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.http.HttpHost;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.utils.URIUtils;
    import org.apache.http.client.utils.URLEncodedUtils;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    public class GooglePlacesRequest {
    
        public static void main(String[] args) throws Exception {
    
            // Web API related
            String apiKey = "YOUR_API_KEY_HERE";
            // search params
            String location = "51.527277,-0.128625";// lat,lon
            String types = "food";
            String name = "pret";
    
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            parameters.add(new BasicNameValuePair("location", location));
            parameters.add(new BasicNameValuePair("radius", "500"));
            parameters.add(new BasicNameValuePair("types", types));
            parameters.add(new BasicNameValuePair("name", name));
            parameters.add(new BasicNameValuePair("sensor", "false"));
            parameters.add(new BasicNameValuePair("key", apiKey));
    
            URL url = new URL(
                    "https://maps.googleapis.com/maps/api/place/search/json");
            URI finalURI = URIUtils.createURI(
                    url.getProtocol(), 
                    url.getHost(),
                    url.getPort(), 
                    url.getPath(),
                    URLEncodedUtils.format(parameters, "UTF-8"), 
                    null);
    
            HttpGet get = new HttpGet(finalURI);
            System.out.println("using getRequestLine(): " + get.getRequestLine());
            System.out.println("using getURI(): " + get.getURI().toString());
    
            DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
            try {
                HttpResponse response = httpClient.execute(get);
                System.out.println("response: "
                        + response.getStatusLine().toString());
                System.out.println( "Response content is:" );
                System.out.println( EntityUtils.toString( response.getEntity() ) );
            } catch (Exception e) {
                System.err.println("HttpClient: An error occurred- ");
                e.printStackTrace();
            }
    
        }
    
    }
    
    导入java.net.URI;
    导入java.net.URL;
    导入java.util.ArrayList;
    导入java.util.List;
    导入org.apache.http.HttpHost;
    导入org.apache.http.HttpResponse;
    导入org.apache.http.NameValuePair;
    导入org.apache.http.client.methods.HttpGet;
    导入org.apache.http.client.utils.URIUtils;
    导入org.apache.http.client.utils.URLEncodedUtils;
    导入org.apache.http.impl.client.DefaultHttpClient;
    导入org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    导入org.apache.http.message.BasicNameValuePair;
    导入org.apache.http.util.EntityUtils;
    公共类GooglePlacesRequest{
    公共静态void main(字符串[]args)引发异常{
    //Web API相关
    String apiKey=“此处为您的API密钥”;
    //搜索参数
    字符串位置=“51.527277,-0.128625”;//纬度,经度
    字符串类型=“食物”;
    String name=“pret”;
    列表参数=新的ArrayList();
    添加(新的BasicNameValuePair(“位置”,位置));
    添加(新的BasicNameValuePair(“半径”,“500”));
    添加(新的BasicNameValuePair(“类型”,类型));
    添加(新的BasicNameValuePair(“name”,name));
    添加(新的BasicNameValuePair(“传感器”、“假”));
    添加(新的BasicNameValuePair(“key”,apiKey));
    URL=新URL(
    "https://maps.googleapis.com/maps/api/place/search/json");
    URI finalURI=URIUtils.createURI(
    url.getProtocol(),
    url.getHost(),
    url.getPort(),
    url.getPath(),
    URLEncodedUtils.format(参数,“UTF-8”),
    无效);
    HttpGet=新的HttpGet(最终);
    System.out.println(“使用getRequestLine():”+get.getRequestLine());
    System.out.println(“使用getURI():”+get.getURI().toString());
    DefaultHttpClient httpClient=新的DefaultHttpClient(新的ThreadSafeClientConnManager());
    试一试{
    HttpResponse response=httpClient.execute(get);
    System.out.println(“响应:
    +response.getStatusLine().toString());
    System.out.println(“响应内容为:”);
    System.out.println(EntityUtils.toString(response.getEntity());
    }捕获(例外e){
    System.err.println(“HttpClient:发生错误-”;
    e、 printStackTrace();
    }
    }
    }
    
    抱怨是不存在的。这很奇怪,因为我可以浏览到它。你需要用实际的API密钥替换“API_密钥”。你有没有可能住在伦敦Flaxman Terrace 16号?@Hidde Nope,我在尤斯顿附近的UCL学习,所以我只需要一些随机的附近坐标。
    import java.net.URI;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.http.HttpHost;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.utils.URIUtils;
    import org.apache.http.client.utils.URLEncodedUtils;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    public class GooglePlacesRequest {
    
        public static void main(String[] args) throws Exception {
    
            // Web API related
            String apiKey = "YOUR_API_KEY_HERE";
            // search params
            String location = "51.527277,-0.128625";// lat,lon
            String types = "food";
            String name = "pret";
    
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            parameters.add(new BasicNameValuePair("location", location));
            parameters.add(new BasicNameValuePair("radius", "500"));
            parameters.add(new BasicNameValuePair("types", types));
            parameters.add(new BasicNameValuePair("name", name));
            parameters.add(new BasicNameValuePair("sensor", "false"));
            parameters.add(new BasicNameValuePair("key", apiKey));
    
            URL url = new URL(
                    "https://maps.googleapis.com/maps/api/place/search/json");
            URI finalURI = URIUtils.createURI(
                    url.getProtocol(), 
                    url.getHost(),
                    url.getPort(), 
                    url.getPath(),
                    URLEncodedUtils.format(parameters, "UTF-8"), 
                    null);
    
            HttpGet get = new HttpGet(finalURI);
            System.out.println("using getRequestLine(): " + get.getRequestLine());
            System.out.println("using getURI(): " + get.getURI().toString());
    
            DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
            try {
                HttpResponse response = httpClient.execute(get);
                System.out.println("response: "
                        + response.getStatusLine().toString());
                System.out.println( "Response content is:" );
                System.out.println( EntityUtils.toString( response.getEntity() ) );
            } catch (Exception e) {
                System.err.println("HttpClient: An error occurred- ");
                e.printStackTrace();
            }
    
        }
    
    }