2016-09-07 153 views
1

我想將數據發佈到正在使用TLS1.2的服務器 當我運行代碼時,我從服務器獲取以下響應。無法使用TLS 1.2 POST POST數據

HttpResponseProxy{HTTP/1.1 415 Unsupported Media Type [Date: Wed, 07 Sep 2016 17:42:57 CST, Server: , Strict-Transport-Security: max-age=63072000; includeSubdomains; preload, Pragma: no-cache, Content-Length: 0, charset: ISO-8859-1, X-ORACLE-DMS-RID: 0, X-ORACLE-DMS-ECID: 343fa6c0-ed24-4003-ad58-342caf000404-00000383, Set-Cookie: JSESSIONID=1ZMIvt1NqrtWpHgHs4mMmYyTPUGTOQgrA9biCE3Dok5v0gDCPXu6!681252631; path=/; secure; HttpOnly;HttpOnly;Secure, Cache-Control: no-store, P3P: policyref="/w3c/p3p.xml", CP="WBP DSP NOR AMT ADM DOT URT POT NOT", Keep-Alive: timeout=5, max=250, Connection: Keep-Alive, Content-Type: text/xml, Content-Language: en] [Content-Type: text/xml,Content-Length: 0,Chunked: false]} 

我正在使用下面的代碼發佈數據到服務器。我正在使用apache httpcomponents-client-4.5.2

private static Registry<ConnectionSocketFactory> getRegistry() throws KeyManagementException, NoSuchAlgorithmException { 
     SSLContext sslContext = SSLContexts.custom().build(); 
     SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, 
       new String[]{"TLSv1.2"}, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); 
     return RegistryBuilder.<ConnectionSocketFactory>create() 
       .register("https", sslConnectionSocketFactory) 
       .register("https", sslConnectionSocketFactory) 
       .build(); 
    } 

    public static void main(String[] args) throws Exception 
    {  
     PoolingHttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager(getRegistry()); 
     clientConnectionManager.setMaxTotal(100); 
     clientConnectionManager.setDefaultMaxPerRoute(20); 
     HttpClient client = HttpClients.custom().setConnectionManager(clientConnectionManager).build(); 
     HttpPost request = new HttpPost("https://someserver.com/dataupload"); 
     File file = new File("C://Nible//code//_client//Request.xml"); 
     MultipartEntityBuilder builder = MultipartEntityBuilder.create();   
     builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
     builder.setContentType(ContentType.TEXT_XML); 
     FileBody fileBody = new FileBody(file); 
     builder.addPart("my_file", fileBody); 
     HttpEntity reqEntity = builder.build(); 
     request.setEntity(reqEntity); 
     HttpResponse response = client.execute(request); 
     System.out.println(response); 
    } 

你能告訴我我做錯了什麼?

我試過使用下面的代碼而不是MultipartEntityBuilder。我仍然收到同樣的錯誤。

EntityBuilder builder = EntityBuilder.create(); 
builder.setFile(file); 
builder.setContentType(ContentType.TEXT_XML); 

如果我發送空白請求到服務器,那麼我也得到相同的錯誤。空白錯誤意味着我不把任何東西在請求剛剛

HttpPost request = new HttpPost("https://someserver.com/dataupload"); 
HttpResponse response = client.execute(request); 
+0

第一個內容類型是'text/xml',第二個是'text/html' ...最好是持久的:選擇一個簡單的(即'text/plain')並且在你的實驗中總是使用相同的。 –

+0

@LittleSanti Iam對不起。它與text/xml相同。我仍然收到同樣的錯誤。你能否提出其他建議? –

+0

而文字/平原? –

回答

1

我懷疑你的代碼行:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();   
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
builder.setContentType(ContentType.TEXT_XML); 

多部分實體不能是內容類型的XML。它們必須是這些類型中的一種:

  • 的multipart /混合
  • 多部分/替代
  • 的multipart /消化
  • 的multipart /並行

(參見RFC 1341 7.2

我猜測你應該爲多部分實體使用其中一種內容類型,並將text/xml設置爲內容類型的單個部件:

FileBody fileBody = new FileBody(file, ContentType.TEXT_XML); 

(另一個問題是,我不認爲有必要派多隻是一個文件:你可以離開了MultipartEntityBuilder對象,並建立一個直接FileEntity。)

+0

我再次在問題中添加了說明。但我仍然遇到同樣的錯誤。 –