java - 向 Chrome 同步服务发送 HTTPS 请求 - 收到错误 404

标签 java sockets ssl

我正在尝试向位于 https://clients4.google.com 的 Chrome 同步服务发送 POST 请求. 我正在使用那段简短的代码来发送我之前在 BURP Suite 的帮助下捕获并保存到文件中的请求。这是 Chrome 在连接到同步服务时发送的内容。 该代码打开一个 SSLSocket,连接到 Chrome 服务器并发送该文件的内容(见下文):

private void sendRequest() {
    SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket socket = (SSLSocket) sslsocketfactory.createSocket("clients4.google.com", 443);
    socket.startHandshake();

    BufferedWriter out = new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
    BufferedReader in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));

    sendMessage(out, new File("request.bin"));
    readResponse(in);

    out.close();
    in.close();
}

private void sendMessage(BufferedWriter out, File request) throws IOException {
    List<String> result = getContents(request);
    for (String line : result) {
        out.write(line + "\r\n");
    }
    out.write("\r\n");
    out.flush();
}

private void readResponse(BufferedReader in) throws IOException {
    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
}

private List getContents(File file) throws IOException {
    List<String> contents = new ArrayList<String>();
    BufferedReader input = new BufferedReader(new FileReader(file));
    String line;
    while ((line = input.readLine()) != null) {
        contents.add(line);
    }
    input.close();
    return contents;
}

request.bin 文件如下所示(这是一个没有 SSL 的明文请求):

POST /chrome-sync/command/?client=Google+Chrome&client_id={VALID_CLIENT_ID} HTTP/1.1
Host: clients4.google.com
Connection: keep-alive
Content-Length: 1730
Authorization: GoogleLogin auth={MY_VALID_AUTH_DATA}
Content-Type: application/octet-stream
User-Agent: Chrome WIN 23.0.1271.97 (171054)
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{binary data}

现在此请求失败,因为服务器返回 HTTP/1.0 404 Not Found。 但是为什么会这样呢? 这与 Chrome 发送的请求完全相同,不是吗?我在这里错过了什么?

最佳答案

在这里回答我自己的问题:问题出在编码上。请求正文中的二进制数据略有修改,导致 Google 服务器响应错误代码 404(这非常令人困惑)。 现在我正在使用正确的编码,一切正常。

关于java - 向 Chrome 同步服务发送 HTTPS 请求 - 收到错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14135336/

相关文章:

c - netdb.h : No such file or directory while trying to run a socket program (in C language) on windows

ASP.NET Request.ServerVariables ["SERVER_PORT_SECURE"] 和负载均衡器代理 SSL

java - 没有 bytearrayinputstream 的 java 中的字符串流

Java JTable 无法设置列宽

linux - C# 资源暂时不可用,在 linux 上的 dotnet core 上阻塞套接字

android - 制作像 Discord 这样的多平台应用程序的技巧

ssl - 带有 AES 的 TLS 或 IPSec 用于物联网安全

java - MySQL JDBC over SSL 问题

java - 单击 RecyclerView 中的 ImageView 时更改图像资源

java - 从不同的对象访问对象的内部类