无法在android或Console中从java调用RestFull API

时间:2016-09-18 06:30:04

标签: java android asp.net-web-api

我想用asp.net webapi调用我创建的restFullAPI,我可以看到来自Web浏览器的结果但是我无法以编程方式从java调用它 这是我的java客户端代码:

public static void main(String[] args){

        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = null;




            String url = "http://localhost:16083/api/get/1";



            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
                    CookiePolicy.RFC_2109);

            httpPost = new HttpPost(url);
            HttpResponse response = httpClient.execute(httpPost);


            HttpEntity entity = response.getEntity();

            // A Simple JSON Response Read
            InputStream instream;
            try {
                instream = entity.getContent();

            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {


        }

    }

响应值为:

  

HTTP / 1.1 405方法不允许[Cache-Control:no-cache,Pragma:   no-cache,允许:GET,Content-Type:application / json;字符集= UTF-8,   过期:-1,服务器:Microsoft-IIS / 10.0,X-AspNet-版本:4.0.30319,   X-SourceFiles:   =?UTF-8 2 B 4 YzpcdXNlcnNcYWRtaW5pc3RyYXRvclxkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDE1XFByb2plY3RzXEFwcGFzZWNcQXBwYXNlY1xhcGlcZ2V0XDE =?=,   X-Powered-By:ASP.NET,日期:Sun,2016年9月18日06:31:52 GMT,   内容长度:73]

1 个答案:

答案 0 :(得分:0)

如果错误显示您不允许使用(发布)的方法。做一个改为。