2011-11-13 17 views
0

httpURLConnectionが404 - Resource not Foundエラーで失敗します。同じURLが私のfirefoxポスターで動作し、200/okを返します。両方のアプリケーション(webappとURL-app)は同じマシンにあります。別のスレッドから、同じURLがブラウザから動作するときに2行以下で動作するようにする必要があることがわかりました -HttpURLConnectionが404エラーで失敗する

urlConnection.setRequestProperty( "User-Agent"、 "Mozilla/5.0(compatible)"); urlConnection.setRequestProperty( "Accept"、 "/");

残念ながら、上記の行を追加しても問題は解決されませんでした。私は以下のコードで何か間違っていますか?有用な提案は非常に高く評価されます。

{ 

String computerName = InetAddress.getLocalHost().getHostName(); 

url = new URL("http://"+computerName+":8080"+"/cerprovider/devices"); 
     urlConnection = (HttpURLConnection)url.openConnection(); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setDefaultUseCaches(false); 
     urlConnection.setUseCaches(false); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.setRequestProperty("Connection",  "Keep-Alive"); 
     urlConnection.setRequestProperty("Content-Length", Integer.toString(Integer.MAX_VALUE)); 
     urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible) "); 
     urlConnection.setRequestProperty("Accept","*/*"); 
     urlConnection.setConnectTimeout(500); 

     // Create I/O streams 
     outStream = new DataOutputStream(urlConnection.getOutputStream()); 
     //inStream = new DataInputStream(urlConnection.getInputStream()); 
     // Send request 
     outStream.writeBytes(<blah>.toString()); 
     outStream.flush(); 
     outStream.close(); 

     BufferedReader in = null; 
     String responseErrorString = ""; 
     _tracer.info("Response code --"+urlConnection.getResponseCode()); 
     InputStream error = urlConnection.getErrorStream(); 
     _tracer.info("Error from the connection --"+error.toString()); 
     StringBuilder buf = new StringBuilder(); 
     for(int c = -1; (c = error.read()) != -1;) 
      buf.append((char)c); 
     responseErrorString = new String (buf); 
     _tracer.info("responseErrorString--"+responseErrorString); 

} 
+0

POST、GETなどどのようなリクエストをしようとしていますか?あなたの要求ペイロードは何ですか? URLをブラウザに直接貼り付けると、フォームデータなしで単純なGETを行い、コードがそれ以上のことをしようとしているように見えることに注意してください。 – wrschneider

+0

私は "POST"をしていますが、私はfirefoxのポスターtool.Itでtext/plainとしてcontent-typeの私の投稿要求を試しています。これはうまく行き、200/okを返します。私のリクエストデータはカンマ区切りの単純な文字列です。 – Chandan

答えて

-2

もう一度リクエストを送信してください。私のプロジェクトでは、Apache HttpClientを使用して、&リクエストを送信し、何度も404エラーがリクエストを再送信して解決します。 (すべてではありません)

また、httpfoxのプラグインをFirefoxで使用して、リクエストを送信したときに正確に何が起きているかを確認します。

404エラーは 'リソースは一時的に利用できません'のため、bに設定されています。

+0

なぜこの行を置く:urlConnection.setRequestProperty( "Content-Length"、Integer.toString(Integer.MAX_VALUE));これは必須ですか? – Aniruddha

+0

私はこれを解決しました。何らかの理由で私のコードが好きではなかった - urlConnection.setRequestProperty( "Accept"、 "*/*");これを変更してテキストを受け入れるかプレーンで問題を解決しました – Chandan

関連する問題