Java URL/HttpURLConnection如何在发布时避免输入流?
是否可以对某个URL执行POST方法请求并避免读取响应 无论我如何努力避免读取响应,除非我读取响应,否则数据似乎永远不会到达服务器。。奇怪 我真的没有必要阅读任何回复数据,因为我要做的就是发布数据。。(无论如何,响应始终为空)Java URL/HttpURLConnection如何在发布时避免输入流?,java,url,http-post,httpurlconnection,Java,Url,Http Post,Httpurlconnection,是否可以对某个URL执行POST方法请求并避免读取响应 无论我如何努力避免读取响应,除非我读取响应,否则数据似乎永远不会到达服务器。。奇怪 我真的没有必要阅读任何回复数据,因为我要做的就是发布数据。。(无论如何,响应始终为空) 您是否尝试过调用con.connect() 否则,它可能会在绝对必须的情况下(缓冲区已满、开始读取响应头等)执行“延迟”操作。在写入后调用getResponseCode() 这还将为您提供404作为响应代码,而不是FileNotFoundException我将con.co
您是否尝试过调用
con.connect()
否则,它可能会在绝对必须的情况下(缓冲区已满、开始读取响应头等)执行“延迟”操作。在写入后调用getResponseCode()
这还将为您提供404作为响应代码,而不是FileNotFoundException
我将con.connect()放在哪里?在我写之前?有什么副作用吗。。我调用flush,希望inputStream被禁用?它不应该填满这个。或者?,顺便说一句,这不是一个保持活动的连接,它只是一个简单的url帖子。connect()不会加速任何事情。相关:getInputStream()在getResponseCode()中调用
URL postURL = new URL("http://www.example.com/test/");
HttpURLConnection con = (HttpURLConnection) postURL.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(false); //why even make this if it doesn't function?
con.setRequestMethod("POST");
//PrintWriter out = new PrintWriter(con.getOutputStream());
OutputStream out = con.getOutputStream();
byte[] /*String postStr*/ bPost = ("foo1="+URLEncoder.encode("bar1")+"&"+
"foo2="+URLEncoder.encode("bar2")+"&"+
"foo3="+URLEncoder.encode("bar3").getBytes();
out.write(bPost);
//out.println(postStr); // send to server
out.flush();
out.close(); // close outputstream
//con.getInputStream().close(); //thought maybe this would help but no change.
/*
//If I uncomment this it will work.
String inputLine=""; //Stores the line of text returned by the server
String resultsPage=""; // Stores the complete HTML results page
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
while ((inputLine = in.readLine()) != null)
resultsPage+=inputLine;
in.close();
*/