我在向 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 是我注入的授权密钥