2012-11-06 6 views
18
import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.net.HttpURLConnection;  
import java.net.URL;  
import java.io.DataOutputStream;   
import java.io.InputStream; 

public class TestingPost { 

public static void main(String args[]) { 

    URL url; 
    HttpURLConnection connection = null; 
    String targetURL=".....";//here is my local server url 
    String urlParameters="{\"clubhash\":\"100457d41b9-ab22-4825-9393-ac7f6e8ff961\",\"username\":\"anonymous\",\"message\":\"simply awesome\",\"timestamp\":\"2012/11/05 13:00:00\"}"; 

    try { 
     //Create connection 
     url = new URL(targetURL); 
     connection = (HttpURLConnection)url.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Content-Type", 
      "application/x-www-form-urlencoded"); 

     connection.setRequestProperty("Content-Length", "" + 
       Integer.toString(urlParameters.getBytes().length)); 
     connection.setRequestProperty("Content-Language", "en-US"); 

     connection.setUseCaches (false); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 

     //Send request 
     DataOutputStream wr = new DataOutputStream (
        connection.getOutputStream()); 
     wr.writeBytes (urlParameters); 
     wr.flush(); 
     wr.close(); 

     //Get Response  
     InputStream is = connection.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     while((line = rd.readLine()) != null) { 
     response.append(line); 
     response.append('\r'); 
     } 
     rd.close(); 
     System.out.println("message="+response.toString()); 

    } catch (Exception e) { 

     e.printStackTrace(); 

    } finally { 

     if(connection != null) { 
     connection.disconnect(); 
     } 
    } 
    } 

} 

Sto tentando di inviare un oggetto JSON utilizzando il metodo HTTP POST. Sopra è il codice ma sto ottenendoHTTP 415 durante l'invio dell'oggetto JSON tramite POST

java.io.IOException: Server returned HTTP response code: 415 for URL: .... 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
at TestingPost.main(TestingPost.java:38)" 

Cosa c'è di sbagliato nel mio codice?

risposta

36

Il codice di risposta HTTP che si ottiene è

415 Unsupported Media Type 

Ciò significa che il server non è in grado di gestire il formato che hai inviato ad esso. La tua richiesta HTTP imposta questa intestazione:

Content-Type: application/x-www-form-urlencoded 

Questo è il tipo di contenuto inviato da un browser, se un modulo viene inviato. Se si desidera inviare JSON, utilizzare questa intestazione:

Content-Type: application/json 
+0

Grazie, funziona davvero !!! –

0

mi è stato correttamente passando

Content-Type: application/json 

Ma il mio server è stato ancora rifiutando la richiesta perché stavo passando anche

Accept: application/json 

Quale non era permesso nel mio caso.

Problemi correlati