如何验证httpURLConnection,发送请求和获取响应

时间:2016-04-18 03:57:31

标签: java android web-services authentication httpurlconnection

我遇到了身份验证问题httpURLConnection。我有一个POST请求的URL,但我需要像用户一样进行身份验证以获得服务器的响应。我尝试使用基本身份验证,但它对我不起作用。无论如何,我得到了responseCode:401 - 未经授权。

您是否知道使用httpURLConnection进行身份验证的其他方法?我做错了什么?

这是我的代码:

Thread networkThread = new Thread() {
  Override
  public void run() {
    try {

      Authenticator.setDefault(new Authenticator() {
        Override
        protected PasswordAuthentication getPasswordAuthentication() {
          Log.d(TAG, " ----------------- =========== >>>> ПАРОЛЬ");
          return new PasswordAuthentication("login", "password".toCharArray());
        }
      });


      URL url = new URL("https://sscale.dk/ro/service2.asmx/stackdata");
      String urlParameters = "ix=123";

      HttpURLConnection con = (HttpURLConnection) url.openConnection();

      String userCredentials = "login:password";
      final String basicAuth = "Basic " + Base64.encode(userCredentials.getBytes(), Base64.NO_WRAP);
      con.setRequestProperty("Authorization", basicAuth);

      // Send post request
      con.setRequestMethod("POST");
      con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      con.setRequestProperty("Content-Length", "" + urlParameters.getBytes().length);
      con.setRequestProperty("Content-Language", "en-US");
      con.setUseCaches(false);
      con.setDoInput(true);
      con.setDoOutput(true);
      DataOutputStream wr = new DataOutputStream(con.getOutputStream());
      wr.writeBytes(urlParameters);
      wr.flush();
      wr.close();

      int responseCode = con.getResponseCode();
      System.out.println("\nSending 'POST' request to URL : " + url);
      System.out.println("Post parameters : " + urlParameters);
      System.out.println("Response Code : " + responseCode);

      BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();

      while ((inputLine = in .readLine()) != null) {
        response.append(inputLine);
      } in .close();

      String result = response.toString();


      Log.d(TAG, " -----------> Мы в интернете: " + result);


    } catch (Exception e) {
      e.printStackTrace();
    }
  }
};
networkThread.start();

这是我的 LOGCAT

System.out: Sending 'POST' request to URL : [https://sscale.dk/ro/service2.asmx/stackdata]

System.out: Post parameters : ix=123

System.out: Response Code : 401

System.err: java.io.FileNotFoundException: https://sscale.dk/ro/service2.asmx/stackdata

System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)

System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)

System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)

System.err:     at net.caustix.redoctoberwoodapp.LoginActivity$1.run(LoginActivity.java:112)

我解决了!答案是我可以访问错误的API页面。我找到了正确的页面,使用post params'用户名'和'密码'登录并且它有效!

0 个答案:

没有答案