Initiate a request to servlet or Webservice in java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.