aspose file tools*
The moose likes I/O and Streams and the fly likes HTTP response code: 403 on http://translate.google.com/translate_tts?q=hello%20world Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "HTTP response code: 403 on http://translate.google.com/translate_tts?q=hello%20world" Watch "HTTP response code: 403 on http://translate.google.com/translate_tts?q=hello%20world" New topic
Author

HTTP response code: 403 on http://translate.google.com/translate_tts?q=hello%20world

axel garcia
Greenhorn

Joined: Sep 02, 2009
Posts: 8
I'm trying to play the mp3 output from the google translator, but I get an 403.
By replacing google with another site that serves up the mp3, it works.

import java.io.*;
import java.net.*;
import javazoom.jl.player.Player;
public class MP3{
private Player player;
public void close(){if(player!=null)player.close();}
public void play(){
try {
URL myURL = new URL("http://translate.google.com/translate_tts?q=hello%20world");
// myURL = new URL("http://anothersite.com/1.mp3");
BufferedInputStream bis = new BufferedInputStream(myURL.openStream());
player = new Player(bis);
}catch (Exception e){
e.printStackTrace();
}
new Thread(){
public void run(){
try {player.play();}
catch(Exception e){System.out.println(e);}
}
}.start();
}
public static void main(String[] args) {
MP3 mp3 = new MP3();
int N = 4000;
double sum = 0.0;
for(int i = 0;i<N;i++){for(int j = 0;j< N;j++){sum += Math.sin(i + j);}}
System.out.println(sum);
mp3.close();
mp3 = new MP3();
mp3.play();
}
}


java.io.IOException: Server returned HTTP response code: 403 for URL: http://translate.google.com/translate_tts?q=hello%20world
java.lang.NullPointerException
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at MP3.play(MP3.java:11)
at MP3.main(MP3.java:31)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Error 403 is "forbidden". You are not allowed to open this file. The cause may be some missing or incorrect headers. Can you open that MP3 in a regular browser?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
axel garcia
Greenhorn

Joined: Sep 02, 2009
Posts: 8
Rob Prime wrote:Error 403 is "forbidden". You are not allowed to open this file. The cause may be some missing or incorrect headers. Can you open that MP3 in a regular browser?


yes.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Then the problem is either caused by headers that are not allowed by Google, or by a proxy you are using with the browser.

For the first you should intercept the headers sent by the browser to Google. You can use the TamperData plugin for Firefox for this. I'm a nice bloke so I did it for you. These are the request headers when sent with Firefox 3.6.3:
For the second, check out java.net.Proxy.

If you still have problems you may want to start using Apache's HttpClient.
axel garcia
Greenhorn

Joined: Sep 02, 2009
Posts: 8
thanks. httpunit works. snippet.

WebConversation webConversation = new WebConversation();
HttpUnitOptions.setScriptingEnabled(false);
WebResponse webResponse = webConversation
.getResponse("http://translate.google.com/translate_tts?q=hello%20world");
BufferedInputStream bis = new BufferedInputStream(webResponse.getInputStream());
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Thanks for sharing the solution
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HTTP response code: 403 on http://translate.google.com/translate_tts?q=hello%20world