Use REST service via a proxy server with URLConnection(Java)

From WABI

Jump to: navigation, search

Contents

Summary

Use REST service via a proxy server with URLConnection (Java)

Description

Specify proxy server as follows to via proxy to use REST service.

URL url = new URL("http", "<proxy_server>", <proxy_port>, "http://xml.nig.ac.jp/rest/Invoke");

Sample program

This program exectes getDDBJEntry of GetEntry, via a proxy server with URLConnection of Java.
Download this porgram

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

public class RestURLConnection {
	public static void main(String[] args) throws IOException {
		//set URL
		URL url = new URL("http", "<proxy_server>",
			<proxy_port>, "http://xml.nig.ac.jp/rest/Invoke");

		//set parameter
		String query = "service=GetEntry&method=getDDBJEntry&accession=AB000200";

		//make connection
		URLConnection urlc = url.openConnection();

		//use post mode
		urlc.setDoOutput(true);
		urlc.setAllowUserInteraction(false);

		//send query
		PrintStream ps = new PrintStream(urlc.getOutputStream());
		ps.print(query);
		ps.close();

		//get result
		BufferedReader br = new BufferedReader(new InputStreamReader(urlc
				.getInputStream()));
		String l = null;
		while ((l=br.readLine())!=null) {
			System.out.println(l);
		}
		br.close();
	}
}

Link

Japanese page

Personal tools