다음 코드를 사용하여 로컬 컴퓨터에서 Http로 문서를 업로드하려고하는데 HTTP 400 잘못된 요청 오류가 발생합니다. 내 소스 데이터는 Json
입니다.HttpPost를 사용하여 문서를 업로드 할 수 없습니다.
URL url = null;
boolean success = false;
try {
FileInputStream fstream;
@SuppressWarnings("resource")
BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Desktop\\test.txt"));
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
buffer.append(line);
}
String request = "http://example.com";
URL url1 = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setDoOutput(true); // want to send
connection.setRequestMethod("POST");
connection.setAllowUserInteraction(false); // no user interaction
connection.setRequestProperty("Content-Type", "application/json");
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.flush();
wr.close();
connection.disconnect();
System.out.println(connection.getHeaderFields().toString());
// System.out.println(response.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
실제로 출력 스트림에 아무 것도 쓰지 않았습니다. 또한 JSON 텍스트를 보내지 않고 자바 객체 그래프를 직렬화하기위한'DataOutputStream'을 사용해서는 안됩니다. –
@ user2724130 정확히 처음 세 속성을 사용합니까? –