nevergiveupzeng

导航

httpClient服务端编写

以前用过HttpClient,给你说几个关键的地方吧:

1. 首先,发送的时候

 

HttpClient client = new HttpClient();
PostMethod method = new PostMethod(URL);//具体method里面还可以设置一下编码,header之类的
//1. 第一种方式,基于Content-Type=‘multipart/form-data’形式的表单
Part[] parts = ...;//FilePart和StringPart都可以放进去
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
//2. 第二种方式,普通表单
NameValuePair[] pairs = ...;//纯参数了,键值对
method.addParameters(pairs);
 
client.executeMethod(method);

 

当然了,还可能有其他的一些形式,这里不过多的说明了,有API可以参考

 

private void parseRequest(HttpServletRequest request) throws Exception {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = upload.parseRequest(request);
        for (int i = 0; i < items.size(); i++) {
            FileItem item = (FileItem) items.get(i);
            if (!item.isFormField()) {
                //文件数据
            } else {
                //普通表单数据
            }
        }
    } else {
        Enumeration en = request.getParameterNames();
        while (en.hasMoreElements()) {
            String paramName = (String) en.nextElement();
            String paramValue = request.getParameter(paramName);
        }
    }
}

 

posted on 2015-09-02 17:20  nevergiveupzeng  阅读(375)  评论(0编辑  收藏  举报