2017-12-01 38 views
0

x-www-form-urlencoded의 본문을 로컬 웹 API에 보내려고합니다. GET을 문제없이 만들 수 있습니다. 여기에 내가 할 것입니다 :content-length in HttpURLConnection

String urlParameters = "user=User&label=GPU+Temp&file=src%2FDataSource0.txt&backgroundColor=rgb(255%2C255%2C255%2C0)&borderColor=rgb(255%2C255%2C255%2C0)&pointBorderColor=rgb(255%2C255%2C255%2C0)&pointHoverBackgroundColor=rgb(255%2C255%2C255%2C0)&pointHoverBorderColor=rgb(255%2C255%2C255%2C0)&min=0&max=100&stepSize=50"; 

    byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8")); 
    int postDataLength = postData.length; 
    String request = "http://192.168.1.30:6262/api/values"; 
    URL url = new URL(request); 
    con = (HttpURLConnection) url.openConnection(); 
    con.setDoOutput(true); 
    con.setInstanceFollowRedirects(false); 
    con.setRequestMethod("POST"); 
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    con.setRequestProperty("charset", "utf-8"); 
    con.setRequestProperty("Content-Length", Integer.toString(postDataLength)); 
    con.connect(); 

java.net.ProtocolException : 내용 길이는 598 바이트를 약속하지만, 0

을 받았다 그래서 내가 어떤 데이터를 전송하고 있지 않다 것을 의미 내 POST, 어떻게 오셨습니까?

+0

쓰기 (postData를) 내가 생각했던 그건 – Mick

답변

1

con.setRequestProperty("Content-Length", Integer.toString(postDataLength));을 사용하면 postDataLength의 길이를 전송하기 만하면 실제로 시체를 설정하지 않습니다. .

당신이 con.getOutputStream()처럼 postData를 보낼 필요가
OutputStream os = con.getOutputStream(); 
os.write(urlParameters.getBytes("UTF-8")); 
os.close(); 
+0

, 신체 : 실제로 connect()를 호출하기 전에이 작업을 수행하는 데 필요한 웹 서비스에 변수를 보내려면 보냈습니다. 시간 내 주셔서 감사합니다. – BeGreen

+0

@BeGreen 문제 없습니다! 다행히 도울 수있어 :) – Signo