|
-
Dec 24th, 2007, 06:09 AM
#1
Thread Starter
New Member
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)
-
Dec 28th, 2007, 05:02 AM
#2
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.
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jan 3rd, 2008, 10:04 AM
#3
Member
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)" );
-
Jan 3rd, 2008, 05:38 PM
#4
Re: java.io.Exception: Server returned HTTP response code: 403
 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)" );
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jan 4th, 2008, 04:17 AM
#5
Member
Re: java.io.Exception: Server returned HTTP response code: 403
 Originally Posted by ComputerJy
 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
Last edited by DonCash; Jan 4th, 2008 at 04:28 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|