HTTP客户端+服务端小工具

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010564366/article/details/73552556

为解决项目中,需要进行的报文对比测试工作,写了一个小的比对工具,做以下记录,方便以后查阅学习,涉及到了http客户端和服务端的构建

这里写图片描述

客户端核心代码:(webservice)

    public static String webServiceRequest(String url, String sysParam,
            String busiParam) throws Exception {
        /** 创建客户端 **/
        WsAccessStub stub = new WsAccessStub(url);
        org.apache.axis2.client.Options o = stub._getServiceClient().getOptions();
        o.setTimeOutInMilliSeconds(300000);

        /** 调用服务 **/
        WsAccessStub.Service service = new WsAccessStub.Service();
        service.setSysParam(sysParam);
        service.setBusiParam(busiParam);
        System.out.println("服务调用中...\n");
        WsAccessStub.ServiceResponse rsp = stub.service(service);
        String secBusiParams = rsp.get_return();

        // 返回结果解密,测试环境暂时不需要
        return secBusiParams;
    }

服务端核心代码:(http,socket实现)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;

import com.asiainfo.simulationTest.respserver.util.SRVUtil;

public class HTTPServer {
    private static final int port = 8888;
    private ServerSocket serverSocket = null;

    public HTTPServer() throws IOException {
        serverSocket = new ServerSocket(port);
        System.out.println("HTTPServer startup OK...");
    }

    public String getRequest(Socket socket) throws IOException {
        InputStream socketIn = socket.getInputStream();
        int size = socketIn.available();
        byte[] requestBuff = new byte[size];
        socketIn.read(requestBuff);
        String rtnstr = new String(requestBuff,"utf-8");

        return rtnstr;
    }

    /**
     * 解析出URL
     */
    public String getURI(String request) {
        String firstLine = request.substring(0, request.indexOf("\r\n"));
        String[] parts = firstLine.split(" ");

        return parts[1];
    }

    /**
     * 解析出业务参数
     */
    public String getIndata(String request) {
        String[] parts = request.split("\r\n");

        return parts[6];
    }

    public String getContentType(String URI) {
        /* 决定HTTP响应正文的类型 */
        String contentType;
        if (URI.indexOf("html") != -1 || URI.indexOf("htm") != -1)
            contentType = "text/html";
        else if (URI.indexOf("jpg") != -1 || URI.indexOf("jpeg") != -1)
            contentType = "image/jpeg";
        else if (URI.indexOf("gif") != -1)
            contentType = "image/gif";
        else
            contentType = "application/json; charset=UTF-8";
        return contentType;
    }

    public InputStream getResponseContent(String URI)
            throws FileNotFoundException {
        InputStream htmlInputStream = new FileInputStream(
                System.getProperty("user.dir") + "/WebRoot" + URI);
        return htmlInputStream;
    }

    public String getResp(String contentType,String sourceId,String flowCode,String inData) {
        /* 创建HTTP响应结果 */
        // HTTP响应的第一行
        reponseHandler rep = new reponseHandler();

        String responseFirstLine = "HTTP/1.1 200 OK\r\n";
        // HTTP响应头
        String responseHeader = "Content-Type:" + contentType + "\r\n\r\n";
        // HTTP响应出参
        String outparam = new String();
        try {
            outparam = rep.getRespParam(sourceId,flowCode,inData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return responseFirstLine + responseHeader+ outparam;
    }


    public void service() throws InterruptedException {
        while (true) {
            Socket socket;
            Map inParam = new HashMap();
            try {
                socket = serverSocket.accept();

                String request = getRequest(socket);
                System.out.println("HttpServer receive request:\n" + request);

                String URI = getURI(request);
                String inData = getIndata(request);
                inParam = SRVUtil.URLRequest(URI);
                String flowCode = inParam.get("BUSICODE").toString();
                String sourceId = inParam.get("SOURCEID").toString();
                String contentType = getContentType(URI);

                OutputStreamWriter outsw = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
                BufferedWriter out = new BufferedWriter(outsw);
                String reponseParam = getResp(contentType,sourceId,flowCode,inData);
                out.write(reponseParam);
                out.flush();
                outsw.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) throws IOException,
            InterruptedException {
        HTTPServer httpServer = new HTTPServer();
        httpServer.service();
    }
}
展开阅读全文

没有更多推荐了,返回首页