HTTP + post + XML

Source: Internet
Author: User

A recent project cannot use WebService. You must use HTTP + post + XML. The specific process is that project a uses Project B's interface to transmit and receive parameters and return values. The following code is available for testing.

1. Request end code

Package action; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstreamwriter; import java.net. httpurlconnection; import java.net. URL; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class C Lientaction extends httpservlet {@ override protected void Service (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {// req. setcharacterencoding ("UTF-8"); stringbuffer reqxml = new stringbuffer ();/* reqxml. append ("<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "); Reqxml. append ("<request>"); reqxml. append ("<meta>"); reqxml. append ("<channeltype> 001 </channeltype>"); reqxml. append ("<reqserialno> 02340385038080 </reqserialno>"); reqxml. append ("<trancode> dx001 </trancode>"); reqxml. append ("<sourceid> 10 </sourceid>"); reqxml. append ("</meta>"); reqxml. append ("<DATA>"); reqxml. append ("<channeltype> 001 </channeltype>"); reqxml. append ("<username> gjjxc </Username> "); Reqxml. append ("<password> gjjxc </password>"); reqxml. append ("<phone> 18662536750 </phone>"); reqxml. append ("<content> test </content>"); reqxml. append ("</data>"); reqxml. append ("</request>"); */inputstream input = This. getclass (). getclassloader (). getresourceasstream ("message.txt"); bufferedreader reqreader = new bufferedreader (New inputstreamreader (input, "UTF-8"); string line2 = NULL; while (Lin E2 = reqreader. Readline ())! = NULL) {reqxml. append (line2);} reqreader. close (); Url url = new URL ("http: // localhost: 8080/smsplant/testservlet"); httpurlconnection UC = (httpurlconnection) URL. openconnection (); UC. setconnecttimeout (10000); UC. setreadtimeout (120000); UC. setrequestmethod ("Post"); // UC. setrequestproperty ("Pragma:", "No-Cache"); // UC. setrequestproperty ("cache-control", "No-Cache"); // UC. setrequestproperty ("content- Type "," text/XML; charset = UTF-8 "); UC. setdooutput (true); outputstreamwriter out = new outputstreamwriter (UC. getoutputstream (), "UTF-8"); system. out. println ("XML sent by the Client:" + reqxml. tostring (); out. write (reqxml. tostring (); out. flush (); out. close (); int state = UC. getresponsecode (); If (State = 200) {// get the returned data bufferedreader in = new bufferedreader (New inputstreamreader (UC. getinputstream (), "UTF-8"); stri NG line = NULL; stringbuffer con = new stringbuffer (); While (line = in. Readline ())! = NULL) {// line is the returned value, which can be used to determine whether it is successful or not. append (line);} system. out. println ("XML received by the Client:" + con. tostring (); In. close (); resp. sendredirect ("http://www.baidu.com ");}}}

2. server code

Package COM. szwx. servlet; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. writer; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. log4j. logger; import COM. szwx. webService. customersmss Ervice; import COM. szwx. webService. impl. customersmsserviceimpl;/*** @ author fanzhigang **/public class testaction extends httpservlet {private customersmsservice; // Private logger log = logger. getlogger (this. getclass (); @ overrideprotected void Service (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {inputstream in = req. getinputstream (); buff Eredreader READ = new bufferedreader (New inputstreamreader (in, "UTF-8"); stringbuffer reqxml = new stringbuffer (); string line = NULL; while (line = read. readline ())! = NULL) {reqxml. append (line);} Read. close (); In. close (); system. out. println ("XML received by the server:" + reqxml. tostring (); customersmsservice = new customersmsserviceimpl (); string res = customersmsservice. sendgjjxc (reqxml. tostring (); // returns resp. setcontenttype ("text/XML; charset = UTF-8"); writer out = resp. getwriter (); system. out. println ("XML returned by the server:" + Res); out. write (RES); out. flush (); out. close ();}}

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.