Encode query with C Sharp
From WABI
Contents |
Summary
Result may not be able to be retrieved normally, if query including non alphanumeric character is used with C#.
Description
You have to encode your query. If qPath is a query to be encoded, please do as follows.
qPath = HttpUtility.UrlEncode(qPath);
Sample program
Following is a sample code that search entries with the condition of Featue Key is 'rRNA', Qualifier name is 'product' and Qualifier value is '16S ribosomal RNA' by using ARSA searchByXMLPath method.
Download this program
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Web;
public class CallARSA{
public static void Main(string[] args) {
/* specify host, url, port number and parameter */
string host = "xml.nig.ac.jp";
string url = "/rest/Invoke";
int port = 80;
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 = HttpUtility.UrlEncode(qPath);
rPath = HttpUtility.UrlEncode(rPath);
String offset = "1";
String count = "10";
string query = "service=ARSA&method=searchByXMLPath&queryPath=" + qPath
+ "&returnPath=" + rPath + "&offset=" + offset + "&count="
+ count;
/* Retrieve IP from host name*/
IPHostEntry hostEntry = Dns.GetHostEntry(host);
IPAddress address = hostEntry.AddressList[0];
IPEndPoint ipe = new IPEndPoint(address, port);
/* Make connection*/
Socket socket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipe);
string request = "POST " + url + " HTTP/1.0\n";
request += "User-Agent: java/socket\n";
request += "User-Agent: java/socket\n";
request += "Content-Type: application/x-www-form-urlencoded\n";
request += "Content-Length:" + query.Length + "\n\n";
request += query;
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
/*send query*/
socket.Send(bytesSent, bytesSent.Length, 0);
/* print result*/
int bytes = 0;
do{
bytes = socket.Receive(bytesReceived, bytesReceived.Length, 0);
Console.Write(Encoding.ASCII.GetString(bytesReceived, 0, bytes));
}while (bytes > 0);
socket.Close();
}
}
