Created
May 22, 2010 13:09
-
-
Save ishuo/410068 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import org.junit.Test; | |
public class HttpURLConnectionDelayTest { | |
static String param1 = "v=1.0&langpair=de%7Cen&q=159%0A&langpair=de%7Cen&q=Vaterrolle%0A&langpair=de%7Cen&q=Informationen+%C3%BCber+die+Rolle+des+Vaters+bei+der+Kindererziehung%0A&langpair=de%7Cen&q=Alle+Dokumente%2C+die+die+spezielle+Rolle+des+Vaters+bei+der+Kindererziehung%0A++++++++untersuchen.+Dazu+geh%C3%B6ren+auch+Dokumente%2C+die+die+Ver%C3%A4nderung+dieser+Rolle+%C3%BCber+die+Zeit%0A++++++++untersuchen.%0A"; | |
static String param2 = "v=1.0&langpair=de%7Cen&q=160%0A&langpair=de%7Cen&q=Prek%C3%A4re+Arbeitsverh%C3%A4ltnisse%0A&langpair=de%7Cen&q=Forschungsarbeiten+und+-ver%C3%B6ffentlichungen+zu+abweichenden+Formen+vom%0A++++++++++++Normalarbeitsverh%C3%A4ltnis%0A&langpair=de%7Cen&q=Welche+%27atypischen%27+Formen+haben+sich+entwickelt%3F+Welches+sind+die+%27prek%C3%A4ren%27+Folgen%0A++++++++++++f%C3%BCr+die+betroffenen+Arbeitnehmer%3F+Welche+Verbesserungen+des+Kranken-%2C+Renten-+und%0A++++++++++++Arbeitslosen-Versicherungsstatus+sind+in+der+Diskussion%3F+Gibt+es+Faktoren%2C+die+die%0A++++++++++++Entwicklungen+stoppen+k%C3%B6nnen%3F%0A"; | |
public static void makeQuery(String parameter) throws Exception | |
{ | |
URL url = new URL("http://ajax.googleapis.com/ajax/services/language/translate"); | |
final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestProperty("referer", "www.test.org"); | |
connection.setRequestMethod("POST"); | |
connection.setDoOutput(true); | |
final PrintWriter writer = new PrintWriter(connection.getOutputStream()); | |
writer.write(parameter); | |
writer.flush(); | |
try { | |
// DEBUG | |
InputStream in = connection.getInputStream(); | |
final String result = inputStreamToString(in); | |
System.out.println(result); | |
} finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html | |
connection.getInputStream().close(); | |
if (connection.getErrorStream() != null) { | |
connection.getErrorStream().close(); | |
} | |
writer.close(); | |
} | |
} | |
// @Test | |
public void testOnlyFirst() throws Exception | |
{ | |
makeQuery(param1); | |
} | |
// @Test | |
public void testOnlySecond() throws Exception | |
{ | |
makeQuery(param2); | |
} | |
// @Test | |
public void testBoth() throws Exception | |
{ | |
makeQuery(param1); | |
makeQuery(param2); | |
} | |
// @Test | |
public void testBothButReversed() throws Exception | |
{ | |
makeQuery(param2); | |
makeQuery(param1); | |
} | |
private static String inputStreamToString(final InputStream inputStream) throws Exception { | |
final StringBuilder outputBuilder = new StringBuilder(); | |
try { | |
String string; | |
if (inputStream != null) { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); | |
while (null != (string = reader.readLine())) { | |
outputBuilder.append(string).append('\n'); | |
} | |
} | |
} catch (Exception ex) { | |
throw new Exception("[google-api-translate-java] Error reading translation stream.", ex); | |
} | |
return outputBuilder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment