【问题标题】:Google Cloud Print throws socket write exception when submitting a printjob谷歌云打印在提交打印作业时抛出套接字写入异常
【发布时间】:2013-06-14 12:10:19
【问题描述】:

我在向 Google 云打印服务提交打印作业时遇到问题。

授权和获取打印机列表非常简单,但是当我尝试提交打印作业时,我得到了 java.net.SocketException: Connection reset by peer: socket write error

查看以下测试应用:

package playground.cloud.google;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;


public class App {


    public static void main(String[] args) throws IOException, URISyntaxException {

        DefaultHttpClient httpclient = new DefaultHttpClient();

        String user = "username@gmail.com";
        String pass = "password";
        String fileToPrint = "miglayout-cheatsheet.pdf";
        String source = "Cloud%20Printing%20Test";

        HttpGet authGet = new HttpGet("https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email="+user+"&Passwd="+pass+"&service=cloudprint&source="+source);

        HttpResponse httpResponse = httpclient.execute(authGet);
        String authResponse = convertStreamToString(httpResponse.getEntity().getContent());
        String authKey = authResponse.substring(authResponse.indexOf("Auth=") + 5);
        System.out.println("Auth key: " + authKey);


        HttpPost printPost = new HttpPost("https://www.google.com/cloudprint/submit?output=json");
        printPost.setHeader("Authorization", "GoogleLogin auth=" + authKey);
        printPost.setHeader("X-CloudPrint-Proxy", source);
        printPost.setHeader("contentType", "application/pdf");


        Path path = Paths.get(fileToPrint);
        MultipartEntity multi = new MultipartEntity();
        multi.addPart("printerid", new StringBody("db6c4137-8833-7d78-5cad-c9f3a9e424ec"));
        multi.addPart("title", new StringBody("miglayout-cheatsheet.pdf"));
        multi.addPart("capabilities", new StringBody("{capabilities=[]}"));
        multi.addPart("content", new FileBody(path.toFile()));


        printPost.setEntity(multi);
        HttpResponse printResponse = httpclient.execute(printPost);

        System.out.println(printResponse);

    }

    public static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
}

依赖项是:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.2.5</version>
</dependency>

我是否遗漏了一些明显的东西?

编辑添加

我可以通过 curl 执行此操作

curl --verbose --insecure --header "X-CloudPrint-Proxy:Cloud Printing Test" --header "Authorization:GoogleLogin auth=%1" --header "contentType:application/pdf" -F "content=@miglayout-cheatsheet.pdf" -F "printerid=db6c4137-8833-7d78-5cad-c9f3a9e424ec" -F "title=miglayout-cheatsheet.pdf" -F "capabilities={capabilities=[]}" https://www.google.com/cloudprint/submit

%1 是我注入的授权密钥

【问题讨论】:

    标签: java google-cloud-print


    【解决方案1】:

    线

    String authKey = authResponse.substring(authResponse.indexOf("Auth=") + 5);
    

    返回 authKey,但在行尾添加 \n。 这会破坏 Http 客户端试图执行的 POST。

    我的 POC 的快速解决方案是添加

    authKey = authKey.replace("\n", "");
    

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 2013-06-30
      • 2012-09-23
      • 2016-04-15
      • 2015-04-23
      • 2012-09-25
      • 2023-03-18
      • 2014-07-21
      相关资源
      最近更新 更多