Created
October 27, 2014 15:59
-
-
Save tckb/3b53193a972f197578e1 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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package com.chemspider.www.examples; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Properties; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author Vishal Siramshetty <vishal-babu.siramshetty[at]charite.de> | |
* @author tckb | |
*/ | |
public class ChemSpiderClient { | |
public static void main(String[] args) throws Exception { | |
String proxy = "proxy.charite.de", | |
port = "8080"; | |
Properties systemProperties = System.getProperties(); | |
systemProperties.setProperty("http.proxyHost", proxy); | |
systemProperties.setProperty("http.proxyPort", port); | |
String someWierdQuery="spiderDrugStuff"; | |
String spiderToken="1234567890"; | |
String searchTheSpiderURL="http://www.chemspider.com/Search.asmx/SimpleSearch?query="+someWierdQuery+"&token="+spiderToken; | |
URL url = new URL(searchTheSpiderURL); | |
HttpURLConnection spiderConnection = (HttpURLConnection) url.openConnection(); | |
spiderConnection.setRequestProperty("User-Agent", "JavaClient/1.0"); | |
spiderConnection.setRequestMethod("GET"); | |
spiderConnection.setDoOutput(true); | |
if(spiderConnection.getResponseCode()== HttpURLConnection.HTTP_OK ){ | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader(spiderConnection.getInputStream())); | |
String inputLine; | |
StringBuffer response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
//print result | |
System.out.println(response.toString()); | |
} | |
else{ | |
System.out.println("Oho! my spidy sense says there is problem! Response code: "+ spiderConnection.getResponseCode(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment