2013-06-18 28 views
0

我無法向Google雲打印服務提交打印作業。提交打印作業時Google雲打印會引發套接字寫入異常

認證和獲得打印機列表很簡單,但我嘗試提交一個PrintJob的那一刻,我收到了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 = "[email protected]"; 
     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 --verbose --insecure --header "X-CloudPrint-Proxy:Cloud Printing Test" --header "Authorization:GoogleLogin auth=%1" --header "contentType:application/pdf" -F "[email protected]" -F "printerid=db6c4137-8833-7d78-5cad-c9f3a9e424ec" -F "title=miglayout-cheatsheet.pdf" -F "capabilities={capabilities=[]}" https://www.google.com/cloudprint/submit 

%1中,我注入

回答

0

行的授權密鑰

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

返回authKey,但添加了\n到行尾。 這會破壞Http客戶端試圖執行的POST。

迅速解決我的POC是添加

authKey = authKey.replace("\n", ""); 
相關問題