Httpcomponents-client-4.3.3 and jdk1.6 httpserver interact data through HTTP

Source: Internet
Author: User

Implement httphandler on the server to process business data

Package Org. shefron. FC. web. HTTP; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. outputstreamwriter; import java.net. uri; import Java. util. list; import Java. util. map. entry; import Org. apache. commons. lang. stringutils; import com.sun.net. httpserver. headers; import com.sun.net. httpserver. httpexchan GE; import com.sun.net. httpserver. httphandler; /*** process the HTTP request and respond ** @ author administrator **/public class busihandler implements httphandler {/*** process the HTTP request and respond */Public void handle (httpexchange exchange) {// If the URL "http://x.x.x.x: Port/HTTP/req. XML ", then the URI is //"/HTTP/req. XML "URI requri = exchange. getrequesturi (); system. out. println ("requesturi =" + requri. tostring (); string method = excha Nge. getrequestmethod (); system. out. println ("method:" + method); string protocol = exchange. getprotocol (); system. out. println ("Protocol:" + Protocol); headers reqheaders = exchange. getrequestheaders (); system. out. println ("requestheaders:"); string host = ""; string reqxmlhead = NULL; For (Entry <string, list <string> entry: reqheaders. entryset () {system. out. println ("Key =" + entry. getkey () + ", value =" + entry. getvalue (); If ("host ". equalsignorecase (entry. getkey () {Host = entry. getvalue (). get (0);} If ("xmlhead ". equalsignorecase (entry. getkey () {reqxmlhead = entry. getvalue (). get (0) ;}} if ("HTTP/1.1 ". equals (Protocol) {protocol = "HTTP";} system. out. println ("------------------------------------"); system. out. println ("url =" + protocol + ": //" + host + requri); system. out. println ("xmlhead =" + reqxmlhead); // process the Request body // handlerequestbody (Exchange. getrequestbody (); justprint (exchange. getrequestbody (); try {headers responseheaders = exchange. getresponseheaders (); responseheaders. set ("Content-Type", "multipart/mixed; charset = UTF-8"); stringbuffer xmlhead = new stringbuffer (); xmlhead. append ("<resinfo>"); xmlhead. append ("<State> true </State>"); xmlhead. append ("</resinfo>"); string headstr = "


Start the process service on the server:

package org.shefron.fc.web.http;import java.net.InetSocketAddress;import java.util.concurrent.Executors;import com.sun.net.httpserver.HttpServer;public class StartService {/** * @param args */public static void main(String[] args) {try{HttpServer httpSer = HttpServer.create(new InetSocketAddress(6060), 0);httpSer.createContext("/myApp", new BusiHandler() );httpSer.setExecutor(Executors.newCachedThreadPool());httpSer.start();}catch(Exception e){}}}


Client HTTP request and response:

Package Org. shefron. FC. web. HTTP; import Java. io. bufferedreader; import Java. io. file; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. NIO. charset. charset; import Java. util. arraylist; import Java. util. list; import Org. apache. HTTP. consts; import Org. apache. HTTP. header; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpversion; import Org. apache. HTTP. namevaluepair; import Org. A Pache. HTTP. statusline; import Org. apache. HTTP. client. clientprotocolexception; import Org. apache. HTTP. client. httpresponseexception; import Org. apache. HTTP. client. entity. urlencodedformentity; import Org. apache. HTTP. client. methods. closeablehttpresponse; import Org. apache. HTTP. client. methods. httppost; import Org. apache. HTTP. entity. contenttype; import Org. apache. HTTP. entity. mime. multipartentitybuilder; import Org. apache. HTTP. entity. mime. content. filebody; import Org. apache. HTTP. entity. mime. content. stringbody; import Org. apache. HTTP. impl. client. closeablehttpclient; import Org. apache. HTTP. impl. client. httpclients; import Org. apache. HTTP. message. basicnamevaluepair; public class clientrequest {public static void Method1 () {closeablehttpclient httpclient = httpclients. createdefault (); try {httppost = new Httppost ("http: // 127.0.0.1: 6060/MyApp/req. XML "); httppost. addheader ("Content-Type", "multipart/form-Data"); stringbuffer xmlhead = new stringbuffer (); xmlhead. append ("<reqinfo>"); xmlhead. append ("<username> admin </username>"); xmlhead. append ("<password> admin </password>"); xmlhead. append ("</reqinfo>"); string headstr = "

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.