java.io.Exception: Server returned HTTP response code: 403
hi can anyone tell me what might be the error in the following code(this is only the part of my code where the error is occuring )
Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost","132.147.160.1");
systemSettings.put("http.proxyPort", "8084");
String record = null;
FileReadTest f = new FileReadTest();
/*opening the text document*/
try{
File fi = new File("gi2.txt");
FileInputStream fis = new FileInputStream(fi);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
while ( (record=dis.readLine()) != null )
{
/*url connection*/
URL url=new URL("http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?db=protein&c_start=1&uids="record"&dopt=fasta&dispmax=1&sendto=t&from=begin&to=end&page= 1");
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
urlConn.setRequestProperty("User-agent","Mozilla/2.0.0.11");
urlConn.connect();
/*creating a text file*/
File create = new File(record+".txt");
try
{
if(!create.exists())
{
create.createNewFile();
}
else
{
System.out.println("file already exists");
}
}
catch(IOException e)
{
System.err.println("cannot create a file");
}
it was returning me the following error
java.io.Exception: Server returned HTTP response code: 403 for URL: http://www.ncbi.nlm.nih.gov/entrez/v...&to=end&page=1
at sun.net.www.protocol.http.HttpURLConnection.getInputStream<HttpURLConnection.java.704>
at eutils.main(eutils.java:136)
Re: java.io.Exception: Server returned HTTP response code: 403
The Error code 403 means "Unauthorized". I think Java was unable to create the file on that url. it's most likely a server settings issue.
Re: java.io.Exception: Server returned HTTP response code: 403
I've had this exact same problem before when I wrote a Java app to connect to Googles search results. Google blocks unauthorized connections and im assuming this site your trying to connect to is doing the same.
I got round this by using the setRequestProperty like you have.
Try changing it to:
Code:
urlConn.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
Re: java.io.Exception: Server returned HTTP response code: 403
Quote:
Originally Posted by DonCash
I've had this exact same problem before when I wrote a Java app to connect to Googles search results. Google blocks unauthorized connections and im assuming this site your trying to connect to is doing the same.
I got round this by using the setRequestProperty like you have.
Try changing it to:
Code:
urlConn.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
:D you should have taken a look at Google's search API before making assumptions. Because it's not always about the request properties. Just a simple look at the documentation would tell you what are you missing is the API key (for free by the way) but instead you tricked the Google server into thinking your application is a web browser (Still a good idea though)
And in case you need a Google search code sample
Re: java.io.Exception: Server returned HTTP response code: 403
Quote:
Originally Posted by ComputerJy
:D you should have taken a look at Google's search API before making assumptions. Because it's not always about the request properties. Just a simple look at the documentation would tell you what are you missing is the API key (for free by the way) but instead you tricked the Google server into thinking your application is a web browser (Still a good idea though)
And in case you need a Google search code sample
Yes I knew about Googles API key. I wanted to use a different method to gather results so I used the setRequestProperty and it worked fine! Infact I think you helped me with it before ComputerJy. PEACE