java.io.IOException: Server returned HTTP response code: 403 for URL

前端 未结 5 1139

I want to download the mp3 file from url : \"http://upload13.music.qzone.soso.com/30671794.mp3\", i always got java.io.IOException: Server returned HTTP response code: 403 f

5条回答
  •  天命终不由人
    2020-12-13 10:51

    Instead of using URLConnection in java, if you use HttpURLConnection you should beable to access the requested web page from java. Try the following code:

     HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); 
     httpcon.addRequestProperty("User-Agent", "Mozilla/4.76"); 
    

    Normal java using urlConnection wont be accepted to access the internet. To access the browser it will need to perform a search without theexception HTTP response code : 403 for URL


    EDIT (@Mordechai): No need to do the casting, just add the user agent.

提交回复
热议问题