2013年11月21日星期四

Java Servlet can take the initiative to initiate a POST request another address it ?

In my Servlet on the server , you need to use another server on the data displayed on a page ,
So, I think by the Servlet to this page to initiate a POST request , then the returned data with Servlet own data together.

I have this need to achieve it ?
how to achieve ?
Thank you !
------ Solution ---------------------------------------- ----
can use HttpURLConnection this class,
detailed usage can Google it, pay attention to consider the timeout situation
------ Solution ------------------------- -------------------
urlStr / / your address
URL url = new URL (urlStr.toString ());
/ / System.out.println (urlStr.toString ());
httpConnection = (HttpURLConnection) url.openConnection ();
httpConnection.getContentEncoding ();
/ / get the return value
java.io.InputStream in = httpConnection.getInputStream ();
BufferedReader breader = new BufferedReader (
new InputStreamReader (in, "gbk"));
String lines = "";
while ((lines = breader.readLine ())! = null) {
str = lines;
System.out.println (str);
}
------ For reference only --------------------------------- ------




Thank you for your answer .
this way , then , POST parameters how to pass ? Thank you !

没有评论:

发表评论