java-网络编程

1、发送post请求
当前引用的jar包
D:\jar\commons-codec-1.10.jar
D:\jar\commons-httpclient-3.1.jar
D:\jar\commons-logging-1.2.jar
public class PostTest {
     public static void main(String[] args) {
          String  url="http://10.3.13.196:8080/portal/v1/alarm";  
          String jsonString=  "{\"telphone\":\"17786618386\",\"channel\":\"sms\",\"content\":\"这是垃圾短信\"}";
          String jsonResult="";
          try {
              HttpClient client=new HttpClient();
              client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
              client.getHttpConnectionManager().getParams().setSoTimeout(18000);
              client.getParams().setContentCharset("UTF-8");
              PostMethod postMethod=new PostMethod(url);
              
              StringRequestEntity requestEntity=new  StringRequestEntity(jsonString, "application/json",  "UTF-8");
              postMethod.setRequestEntity(requestEntity);
              
              int  status=client.executeMethod(postMethod);
              
              if(status==HttpStatus.SC_OK){
                   jsonResult=postMethod.getResponseBodyAsString();
              }else{
                   System.err.println("error");
              }
              
          } catch (Exception e) {
              // TODO: handle exception
          }
          System.out.println(jsonResult);
     }
}
参考: https://www.cnblogs.com/Marydon20170307/p/9202987.html

转载于:https://www.cnblogs.com/yanliang12138/p/10721584.html

你可能感兴趣的:(java-网络编程)