• Java HttpClient PostMethod


    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.junit.Test;
    
    /**
     * @author ceshi
     * @Title: JunitTest
     * @ProjectName test
     * @Description: TODO
     * @date 2018/6/1416:39
     */
    public class JunitTest {
        @Test
        public void test(){
            HttpClientPostMethod("http://localhost:8080/test/api","你好");
        }
    
    
        public String  HttpClientPostMethod(String url,String data){
            //1.创建 HttpClient
            HttpClient httpClient = new HttpClient();
            // 2.构造PostMethod的实例
            PostMethod postMethod = new PostMethod(url);
            //3.请求参数
            postMethod.addParameter("data", data);
            String responseMsg = "";
            try {
                httpClient.executeMethod(postMethod);
                responseMsg = postMethod.getResponseBodyAsString().trim();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 4.释放连接
                postMethod.releaseConnection();
            }
            return  responseMsg;
        }
    
    }

    maven pom需要添加

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
       <version>4.5.3</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
  • 相关阅读:
    补结对编程四则运算(结对伙伴)
    alpha发布(技术随笔)
    第四周 更新Scrum站立会议
    第四周 技术随笔psp
    第四周 课堂Scrum站立会议
    更新-补四则运算(结对编程)
    补交git、ssh
    补第二周四人小组WBS/NABCD
    补充第二周燃尽图
    修改第二周站立会议
  • 原文地址:https://www.cnblogs.com/qinxu/p/9184136.html
Copyright © 2020-2023  润新知