星空网 > 软件开发 > Java

关于http请求调用接口

1.这是通过json传值进行调用

(1)public static void appadd() {
        try {
            //创建连接
            URL url = new URL(ADD_URL);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.connect();
            //POST请求
            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());
            JSONObject obj = new JSONObject();
            obj.element("mesConent",  java.net.URLEncoder.encode("郭郭"));
            obj.element("phoneList", "15662165280");
          

            out.writeBytes(obj.toString());
            out.flush();
            out.close();
            //读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String lines;
            StringBuffer sb = new StringBuffer("");
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                sb.append(lines);
            }
            System.out.println(sb);
            reader.close();
            // 断开连接
            connection.disconnect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

(2)是json字符窜

  public static String doHttpPost(String          System.out.println("发起的数据:"+         InputStream instr  = null;
         java.io.ByteArrayOutputStream out = null;              
          try{                          
                 URL url = new URL(URL);                
                 URLConnection urlCon = url.openConnection();               
                 urlCon.setDoOutput(true);              
                 urlCon.setDoInput(true);               
                 urlCon.setUseCaches(false);    
                 urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");       
                 DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());            
                 printout.writeBytes(                 printout.flush();              
                 printout.close();              
                 //读取响应
                 BufferedReader reader = new BufferedReader(new InputStreamReader(
                         urlCon.getInputStream()));
                 String lines;
                 StringBuffer sb = new StringBuffer("");
                 while ((lines = reader.readLine()) != null) {
                     lines = new String(lines.getBytes(), "utf-8");
                     sb.append(lines);
                 }
                 System.out.println(sb);
                 reader.close();    
             return sb.toString();
          }catch(Exception e){              
                 e.printStackTrace();
                return "0";
          }             
          finally {             
                 try {          
                        out.close();   
                        instr.close();  
                         
                 }catch (Exception ex) {      
                        return "0";
                     }                  
                 }                 
          }             

2  通过参数传值例如url传值 ?a=?&b=?

利用httpcliend

String url="";
     HttpClient client1=new HttpClient();
     client1.setConnectionTimeout(30000);
     PostMethod postMethod = new PostMethod(url);
     client1.getParams().setContentCharset("UTF-8");
     postMethod.setParameter("businesId", "1");
     postMethod.setParameter("itemId", "2");

     try {
        
        client1.executeMethod(postMethod);
         JSONObject result_code=  (JSONObject) JSONObject.parse(postMethod.getResponseBodyAsString());
         String result_id=result_code.getString("state");
         String error_msg=result_code.getString("error");
         if(result_id.equals("200")){
            
         }
    } catch (HttpException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    } catch (IOException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }

 

注意的事情

就是在提交请求的时候会出现编码规范关于http请求调用接口

application/x-www-form-urlencoded    在发送前编码所有字符(默认)
multipart/form-data     不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。
text/plain     空格转换为 "+" 加号,但不对特殊字符编码。

这些是httpclient调用最基本的包

关于http请求调用接口

 




原标题:关于http请求调用接口

关键词:http

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

ios如何下载tiktok:https://www.goluckyvip.com/tag/82209.html
Lazada新动态:https://www.goluckyvip.com/tag/8221.html
tiktok安卓直装版:https://www.goluckyvip.com/tag/82210.html
安卓tiktok加速器:https://www.goluckyvip.com/tag/82211.html
tiktok国际版国内可用下载:https://www.goluckyvip.com/tag/82212.html
tiktok破解版最新下载:https://www.goluckyvip.com/tag/82213.html
九华山离哪个城市近?:https://www.vstour.cn/a/408248.html
你更喜欢一个人旅行,还是一群人旅行? :https://www.vstour.cn/a/408249.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流