HttpClient initiates a request to servlet or Webservice
1. Send the request servlet, similar to ajax asynchronous call
Import java.net. HttpURLConnection;
Public void do (){
String url = "http: // localhost: 8081/servlet. do ";
Java.net. URL initStaticPageUrl = new java.net. URL (url );
Java.net. HttpURLConnection httpConnection = (java.net. HttpURLConnection) initStaticPageUrl. openConnection ();
HttpConnection. getResponseCode ();
}
2. Send xml files to servlet or Webservice in java
Import java. io. BufferedInputStream;
Import java. io. BufferedReader;
Import java. io. FileReader;
Import java. io. OutputStream;
Import java.net. HttpURLConnection;
Public void send (){
Try {
String xmlpath = Main. homepath + "/oneword. xml"; // path for saving the xml file
// Read the xml file
BufferedReader fin = new BufferedReader (new FileReader (xmlpath ));
StringBuffer stringbuffer = new StringBuffer ();
Int k = 0;
While (k! =-1)
{
K = fin. read ();
If (k! =-1)
Stringbuffer. append (char) k );
}
Fin. close ();
// Send the oneword. xml file
String xml = stringbuffer. toString ();
String url2 = "http://www.bkjia.com/word.html ";
Java.net. URL pageUrl = new java.net. URL (url2 );
HttpURLConnection connection = (HttpURLConnection) pageUrl. openConnection ();
Connection. setRequestMethod ("POST ");
Connection. setDoInput (true );
Connection. setDoOutput (true );
Connection. setRequestProperty ("Content-Type", "text/xml ");
Byte [] contentbyte = xml. getBytes ();
Connection. setRequestProperty ("Content-Length", "" + contentbyte. length );
Connection. setRequestProperty ("Cache-Control", "no-cache ");
Connection. setRequestProperty ("Pragma", "no-cache ");
Connection. setRequestProperty ("Expires", "-1 ");
OutputStream out = connection. getOutputStream ();
Out. write (contentbyte );
/**
OutputStreamWriter wout = new OutputStreamWriter (out, "UTF-8 ");
Wout. write ();
Wout. flush ();
**/
Out. flush ();
Out. close ();
BufferedInputStream bis = new BufferedInputStream (connection. getInputStream ());
Bis. close ();
Stringbuffer = null;
} Catch (Exception e)
{
E. printStackTrace ();
}
}
Author: feixiangdexin123087