简单的Httpserver,Httpclient,xml
程序员文章站
2022-06-14 16:08:40
...
简单演示Http SERVER,CLIENT发送接收xml的流程。
XMLSERVER:
监听某端口:接收xml,并解析。
public class XmlServer {
private int port;
public XmlServer() {
}
public XmlServer(int port) {
this.port = port;
}
public void start(){
try {
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/",new HttpHandler() {
@Override
public void handle(HttpExchange he) throws IOException {
try {
String ip = he.getRemoteAddress().getAddress().getHostAddress();
System.out.println(ip);
InputStream input_stream = he.getRequestBody();
// BufferedReader reader = new BufferedReader(new InputStreamReader(input_stream));
// StringBuilder str_builder = new StringBuilder();
//
// String line = null;
//
// while ((line = reader.readLine()) != null)
// {
// str_builder.append(line + "\n");
// }
//
// System.out.println("接收:"+str_builder);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new InputStreamReader(input_stream));
Element rootElm = document.getRootElement();
String username = rootElm.element("username").getText();
String password = rootElm.element("password").getText();
System.out.println(username);
System.out.println(password);
//登录,
String ret = "OK";
he.sendResponseHeaders(200, ret.getBytes("utf-8").length);
OutputStream os = he.getResponseBody();
os.write(ret.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
server.start();
System.out.println("开始监听"+port+"端口");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new XmlServer(9988).start();
}
}
XmlClient:
发送请求
[java] view plaincopy
public class XmlClient {
public static void main(String[] args) {
try {
String xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<message>" +
"<username>ayli</username>" +
"<password>liya</password>" +
"</message>";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:9988");
StringEntity entity = new StringEntity(xml, "text/html", "utf-8");
post.setEntity(entity);
ResponseHandler<String> response = new BasicResponseHandler();
String ret = client.execute(post, response);
System.out.println(ret);
} catch (Exception e) {
e.printStackTrace();
}
}
}
注:需要dom4j-1.6,httpclient-4.1相关的jar包
XMLSERVER:
监听某端口:接收xml,并解析。
public class XmlServer {
private int port;
public XmlServer() {
}
public XmlServer(int port) {
this.port = port;
}
public void start(){
try {
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/",new HttpHandler() {
@Override
public void handle(HttpExchange he) throws IOException {
try {
String ip = he.getRemoteAddress().getAddress().getHostAddress();
System.out.println(ip);
InputStream input_stream = he.getRequestBody();
// BufferedReader reader = new BufferedReader(new InputStreamReader(input_stream));
// StringBuilder str_builder = new StringBuilder();
//
// String line = null;
//
// while ((line = reader.readLine()) != null)
// {
// str_builder.append(line + "\n");
// }
//
// System.out.println("接收:"+str_builder);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new InputStreamReader(input_stream));
Element rootElm = document.getRootElement();
String username = rootElm.element("username").getText();
String password = rootElm.element("password").getText();
System.out.println(username);
System.out.println(password);
//登录,
String ret = "OK";
he.sendResponseHeaders(200, ret.getBytes("utf-8").length);
OutputStream os = he.getResponseBody();
os.write(ret.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
server.start();
System.out.println("开始监听"+port+"端口");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new XmlServer(9988).start();
}
}
XmlClient:
发送请求
[java] view plaincopy
public class XmlClient {
public static void main(String[] args) {
try {
String xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<message>" +
"<username>ayli</username>" +
"<password>liya</password>" +
"</message>";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:9988");
StringEntity entity = new StringEntity(xml, "text/html", "utf-8");
post.setEntity(entity);
ResponseHandler<String> response = new BasicResponseHandler();
String ret = client.execute(post, response);
System.out.println(ret);
} catch (Exception e) {
e.printStackTrace();
}
}
}
注:需要dom4j-1.6,httpclient-4.1相关的jar包