java httpclient POST和GET请求 发送JSON

第一步导包

导入的jar包有  1:httpclient  2:httpcore  3commons-logging-api  4:json包 (转换json的jar包是很多的每个系统的方法都是不一样) json-io-4.3.0


第二步 写代码

GET请求

    

public static void doGet(String url) throws Exception{

  //url  路径

    HttpClient client = new DefultHttpClient();

   HttpGet getmeth = new HttpGet(url);

  try {

        HttpResponse statueCode = cleint.execute(getmeth);

      System.out.println(response1.getStatusLine());//状态码  200成功   500服务器内部报错这个时候要看服务端的报错日志  404 路径错误

   }catch(IOException e){

  e.printStackTrace();

  }

}



POST请求


public  static void doPost(String url,String param) throws Exception{

HttpClient client = new DefultHttpClient();

HttpPost postmeth = new HttpPost(url);

postmeth.setEntity(new StringEntity(param));    //post请求参数  param可以在请求之前进行json转换

try{

      HttpResponse statueCode = cleint.execute(getmeth);

      System.out.println(response1.getStatusLine());//状态码  200成功   500服务器内部报错这个时候要看服务端的报错日志  404 路径错误




}catch(IOException e){

e.printStackTrace();

}


}


转json方法


public static String json( String param){

List<String> list = new ArrayList<String>();

list.add(param);

String json = JsonWriter.objectToJson(list)'

return json;

}




main方法调用请求


public static void main(String[] args) {

json(param);

try {
                doGet(url);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }




try {
                doPost(url ,param);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


}



webservice 基础知识


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值