调用post方法

时间:2018-05-09 13:43:06

标签: android asp.net-web-api

我无法从Android应用调用post方法 当我从chorome浏览器调用以下URL时,它可以正常工作

  

http://172.16.2.113/api/DigitechProductsAC/PostService?ctcCompany=test&name=test&mobile=test1&description=test2&address=test3&productCompany=test4&extra1=asdf&extra2=3434

我收到了以下回复

  

请求网址:http://172.16.2.113/api/DigitechProductsAC/PostService?ctcCompany=test&name=test&mobile=test1&description=test2&address=test3&productCompany=test4&extra1=asdf&extra2=3434   请求方法:POST   响应时间:6.689秒   响应状态:200 - 确定

但是从android它不起作用..我目前的android代码是:

String url1= "http://172.16.2.113/api/DigitechProductsAC/PostService";
            String  data= "ctcCompany=test&name=test&mobile=test1&description=test2&address=test3&productCompany=test4&extra1=asdf&extra2=zzz";

        String response = "";
        try {
            URL url = new URL(url1);
            HttpURLConnection   connection = (HttpURLConnection) url.openConnection();
            String urlParam = data;
            connection.setRequestMethod("POST");
            connection.setRequestProperty("USER-AGENT","Mozilla/5.0");
            connection.setRequestProperty("ACCEPT-LANGUAGE","en-US,en;0.5");

            connection.setDoOutput(true);
            DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
            dataOutputStream.writeBytes(urlParam);
            dataOutputStream.flush();
            dataOutputStream.close();

            int responseCode= connection.getResponseCode();
            if (responseCode==200)

         return  "OK";
            else
                return "";
        }catch (Exception ex)
        {
            return null;
        }

它没有到达服务器!!

0 个答案:

没有答案