XMLHttpRequest is very handy in Javascript to make request to web services. But, how to do it in back end server side?

C# ~ HttpWebRequest

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

// Get response
using (response = request.GetResponse() as HttpWebResponse)
{
	// Get the response stream
	StreamReader reader = new StreamReader(response.GetResponseStream());

	// Console application output
	return reader.ReadToEnd();
}

Java ~ URLConnection

URL _url = new URL(url);
URLConnection connection = _url.openConnection();
connection.connect();

BufferedReader rd  = new BufferedReader(
		new InputStreamReader(connection.getInputStream(),Const.CHARSET));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = rd.readLine()) != null){
	  sb.append(line + '\n');
}
return sb.toString();
Advertisements