0

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 は、私が挿入した認証キーです

4

1 に答える 1