Java中的HTTP GET请求从App Engine

时间:2015-04-24 15:42:57

标签: java google-app-engine get put httpconnection

我正在从App Engine网址

请求json文件

http://1-1-26a.wordbuzzweb.appspot.com/json/level-images.json

文件编码为UTF-8,没有BOM。如果我在本地磁盘上查看此文件,它的大小为12414字节。如果我在Chrome中获取该文件,则可以很好地读取它。如果我然后保存它是12414字节。但是,如果我尝试在java中使用GET请求下载文件,则只返回780个字节,返回的数据似乎毫无意义。

我尝试了几种不同类型的get请求,我在其他方面完全有效地使用了这两种方法。 GET请求的响应代码是200.有趣的是,如果我执行的POST没有内容而不是GET,那么我会得到有效的响应。

如果我从Google Drive上的this网址下载该文件,那么下面的GET方法可以正常运行。

编辑此代码现在正在运行,但是,这是一个反复出现的问题。如果有人有任何想法可能会导致它,那么请说出来!

这不起作用

public static String doGetSync(String urlToRead) throws IOException {
    URL url = new URL(urlToRead);

    InputStream is = url.openStream();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[BUFFER_SIZE];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();

    byte[] bytes = buffer.toByteArray();

    return new String(bytes, "UTF-8");
}

这也不是

public static String doGetSync2(String urlToRead) throws IOException {
    final String charset = "UTF-8";
    // Create the connection
    HttpURLConnection connection = (HttpURLConnection) new URL(urlToRead).openConnection();

    // Check the error stream first, if this is null then there have been no issues with the request
    InputStream inputStream = connection.getErrorStream();
    if (inputStream == null)
        inputStream = connection.getInputStream();

    // Read everything from our stream
    BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream, charset));

    String inputLine;
    StringBuilder response = new StringBuilder();

    while ((inputLine = responseReader.readLine()) != null) {
        response.append(inputLine);
    }
    responseReader.close();

    return response.toString();
}

此代码有效

public static String doPostSync(final String url, final String content) throws IOException {
    final String charset = "UTF-8";
    // Create the connection
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    // setDoOutput(true) implicitly set's the request type to POST
    connection.setDoOutput(true);
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setRequestProperty("Content-type", "application/json");

    // Write to the connection
    OutputStream output = connection.getOutputStream();
    output.write(content.getBytes(charset));
    output.close();

    // Check the error stream first, if this is null then there have been no issues with the request
    InputStream inputStream = connection.getErrorStream();
    if (inputStream == null)
        inputStream = connection.getInputStream();

    // Read everything from our stream
    BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream, charset));

    String inputLine;
    StringBuilder response = new StringBuilder();

    while ((inputLine = responseReader.readLine()) != null) {
        response.append(inputLine);
    }
    responseReader.close();

    return response.toString();
}

0 个答案:

没有答案