Encode query with Java
From WABI
Contents |
Summary
Result may not be able to be retrieved normally, if query including non alphanumeric character is used with Java.
Description
You have to encode your query. If qPath is a query to be encoded, please do as follows.
qPath = URLEncoder.encode(qPath, "UTF-8");
Sample program
Following is a sample code that search entries with the condition of Feature Key is 'rRNA', Qualifier name is 'product' and Qualifier value is '16S ribosomal RNA' by using ARSA searchByXMLPath method.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.URLEncoder;
public class Rest {
public static void main(String[] args) throws IOException {
// set server
String host = "xml.nig.ac.jp";
int port = 80;
// set API server
String url = "/rest/Invoke";
String qPath = "/ENTRY/DDBJ/feature-table/feature/f_key=='rRNA' AND ";
qPath += "(/ENTRY/DDBJ/feature-table/feature{/f_key=='rRNA' AND ";
qPath += "/f_quals/qualifier{/q_name=='product' AND /q_value='16S ribosomal RNA'}}) ";
String rPath = "/ENTRY/DDBJ/primary-accession,/ENTRY/DDBJ/definition";
qPath = URLEncoder.encode(qPath, "UTF-8");
rPath = URLEncoder.encode(rPath, "UTF-8");
String offset = "1";
String count = "10";
// set parameter
String query = "service=ARSA&method=searchByXMLPath&queryPath=" + qPath
+ "&returnPath=" + rPath + "&offset=" + offset + "&count="
+ count;
// open socket
Socket socket = new Socket(host, port);
// send query
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
PrintStream pw = new PrintStream(socket.getOutputStream());
pw.print("POST " + url + " HTTP/1.0\n");
pw.print("User-Agent: java/socket\n");
pw.print("Content-Type: application/x-www-form-urlencoded\n");
pw.print("Content-Length:" + query.length() + "\n\n");
pw.print(query);
// get result
String l = null;
while ((l = br.readLine()) != null) {
System.out.println(l);
}
pw.close();
br.close();
}
}
