【问题标题】:Rest api call using post method of HttpsURLConnection returns 404 in as response使用 HttpsURLConnection 的 post 方法的 Rest api 调用返回 404 作为响应
【发布时间】:2017-09-30 18:17:57
【问题描述】:

密码和用户名在邮递员中工作正常,但每当我从应用程序调用时,它都会返回 404 响应。而且在邮递员中也不需要任何标题。

我已尝试更改代码行的顺序,但似乎没有任何效果。

互联网的android权限也被纳入清单。

    pin = "pin_code = 1111";
    uname = "username = bruce";
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            HttpsURLConnection connect = null;
            try {
                getApi = new URL("https://portal.hal.com/users/api/patient/login/with_username/");

                connect = (HttpsURLConnection) getApi.openConnection();
                connect.setDoInput (true);
                connect.setDoOutput(true);
                connect.setRequestMethod("POST");
                connect.connect();
                connect.getOutputStream().write(pin.getBytes());
                connect.getOutputStream().write(uname.getBytes());
                connect.getOutputStream().close();

                if (connect.getResponseCode() == 200 ) {
                    Log.d(TAG,"DONE");
                }  if (connect.getResponseCode() == 404){
                    Log.d(TAG,"404 error");
                }

【问题讨论】:

  • 您的网址可能有误
  • 我在这里更改了网址,因为它是机密的,但真正的网址在邮递员中可以正常工作
  • 如果你使用 connect.setRequestMethod("POST") 不要使用 connect.setDoInput (true);这有时可能会覆盖 POST 方法到 GET。尝试同时删除 connect.setDoInput (true);和 connect.setDoOutput(true);
  • 我试过了。仍然不起作用 404 显示错误
  • @SadidWinchester 检查您提供的参数是否正确发送。调试和检查。

标签: android rest httprequest httpurlconnection


【解决方案1】:

发现错误。 in 用户名和密码字符串都在一个字符串中发送。

 String parameter = "pin_code=4444&username=user";
 connect.getOutputStream().write(parameter.getBytes());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多