httpclient3.1 use summary

 
Jakarta's httpclient3.1 is the latest version, the project needs to use the program to simulate the browser's GET and POST actions. I encountered many problems during use.
1. The POST submission with attachments
    initially used the MultipartPostMethod class, which is now deprecated. API description: Deprecated. Use MultipartRequestEntity in conjunction with PostMethod instead. Using the functions that PostMethod can achieve, there is no need to get another MultipartPostMethod. The following is the simplest example:

PostMethod post = new PostMethod();
        NameValuePair[] pairs = new NameValuePair[2];
        pairs[0] = new NameValuePair("para1", "value1");
        pairs[0] = new NameValuePair("para2", "value2");
        post.setRequestBody(pairs);
        HttpClient client = new HttpClient();
        try {
            client.executeMethod(post);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
   This is for general form submission, and this form does not have attachments. If there is an attachment, this method will not work, and the parameters uploaded by the attachment and the ordinary parameters cannot be obtained from the server together. The org.apache.commons.httpclient.methods.multipart package is designed to handle the multi-form parameter case of file uploads. The most important class is Part (representing a kind of post object), which has two important subclasses: FilePart and StringPart, one is the parameter of the file, and the other is the ordinary text parameter. Its typical usage is as follows:
String url = "http://localhost:8080/HttpTest/Test";
         PostMethod postMethod = new PostMethod(url);
        
         StringPart sp = new StringPart("TEXT", "testValue");
         FilePart fp = new FilePart("file", "test.txt", new File("./temp/test.txt"));
        

                 .getParams());
         postMethod.setRequestEntity(mrp);
        
         //Execute postMethod
         HttpClient httpClient = new HttpClient();
         try {
            httpClient.executeMethod(postMethod);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch ( IOException e) {
            e.printStackTrace();
        }
    After the second line PostMethod postMethod = new PostMethod();, someone said you need to use postMehtod.setRequestHeader("Content-type", "multipart/form-data"); Content- type of the request to change. But I didn't add this sentence in the process of using it. I checked that the default Content-type of httpCleint is application/octet-stream. Should have no effect. For MIME type requests, httpclient recommends wrapping them all with MulitPartRequestEntity, which is the usage above.

2. The processing problem of parameter Chinese
    The default encoding of httpclient is ISO-8859-1, so it must not support Chinese parameters. To quote this article: http://thinkbase.net/w/main/Wiki?HttpClient+POST+%E7%9A%84+UTF-8+%E7%BC%96%E7%A0%81%E9%97 %AE%E9%A2%98, according to the author, can solve the problem of Chinese encoding normally. The most critical of them is to modify a method implementation of the EncodingUtil class. In addition, the construction methods of FilePart and StringPart have a parameter specified by encoding. In order to reduce the occurrence of problems, it is recommended that all of them have a unified encoding, including postMethod.getParams(). Example:
String url = "http://localhost:8080/HttpTest/Test";
         PostMethod postMethod = new PostMethod(url);
        
         StringPart sp = new StringPart("TEXT", "testValue", "GB2312");
         FilePart fp = new FilePart("file", "test.txt", new File("./temp/test.txt"), null, "GB2312");
        
         postMethod.getParams().setContentCharset("GB2312");

                 .getParams());
         postMethod.setRequestEntity(mrp);
        
         //执行postMethod
         HttpClient httpClient = new HttpClient();
         try {
            httpClient.executeMethod(postMethod);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326277185&siteId=291194637