2011-08-05 8 views
2

URLにPOSTメソッドリクエストを行い、応答を読み取らないようにすることはできますか?どんなに私はデータは私が応答を読んでいない限り、サーバーに届くように見えることはありません応答を読んで回避しよういかに難しいJava URL/HttpURLConnection投稿中にInputStreamを回避する方法は?

...奇妙な?

私がやってされるすべてのデータを掲示されるように私は本当に

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(); 
*/ 
+0

関連:[HttpURLConnectionのを安全に使用する](http://stackoverflow.com/questions/4767553)私はcon.connectを入れてください – McDowell

答えて

3

コールgetResponseCode()

これはまた、あなたの応答コードではなく、FileNotFoundExceptionとして404を与えるだろう。

+0

getInputStream()はgetResponseCode()で呼び出されます。 – hoi

-1

(応答は常にとにかく空白になります)。..任意の応答データを読み込むにはポイントを持っていないあなたはcon.connect()を呼び出してみましたか?

それ以外の場合は、おそらく「いい加減に」それは絶対に(レスポンスヘッダなどを読み始めて、完全なPOSTバッファ)に持っていたときに行います。書き込み後の

+0

()?私は書く前に?悪い副作用はありますか? ..私はフラッシュを呼び出し、うまくいけば、inputStreamは無効になっていますか?それはそれを埋めるべきではありません。または?ところでこれは何も加速しないURLには、単純なポストだキープアライブ接続... – SSpoke

+0

接続()ではありません。 – EJP

関連する問題