【问题标题】:Request failed with Authentication in ksoap2请求失败,在 ksoap2 中进行身份验证
【发布时间】:2018-11-30 21:55:28
【问题描述】:

我在连接到 Web 服务时遇到了身份验证问题。当我尝试通过网络浏览器打开它时,它要求我使用“登录名”和“密码”登录。 我正在使用下面的代码进行身份验证,但它不起作用。我收到 401。我尝试了所有找到的方法,但仍然无法连接。

String res = "sample string";
        SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(WSDL);

        List<HeaderProperty> headers = new ArrayList<HeaderProperty>();
        headers.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("my_username:my_password".getBytes())));

        androidHttpTransport.debug = true;


        try {

            androidHttpTransport.call(SOAP_ACTION, envelope, headers);
            SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
            res = resultsRequestSOAP.toString();


            System.out.println("Response: " + resultsRequestSOAP.toString());

        } catch (Exception e) {  //here comes org.ksoap2.transport.HttpResponseException: HTTP request failed, HTTP status: 401
            Log.d(TAG, e.getMessage());
        }
        return res;

【问题讨论】:

  • 您从 Web 服务得到什么样的响应?
  • HTTP request failed, HTTP status: 401 ,字符串值不变
  • 如果有用的话,我会在 doInBackground() 内调用它
  • 我必须添加第三个,因为我无法编辑最后一个:我使用this 得到SOAP_ACTION, WSDL, NAMESPACE

标签: android web-services basic-authentication android-ksoap2


【解决方案1】:

如果有人在看这个问题,我可以从服务器获取日志,这就是我得到的:

2018-11-30 13:13:36 YY.YY.YY.YY POST /MicrosoftDynamicsAXAif50/servicee.svc - 89 - XX.XX.XX.XX ksoap2-android/2.6.0+ - 401 2 5 15

YY - 服务器地址,XX - 我的
在我的地址应该是我的用户名之前,不应该有你可以看到的 lib 名称。我怎样才能让它工作?因为我总是会被服务器拒绝。

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2010-10-20
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2018-11-14
    • 2015-12-05
    相关资源
    最近更新 更多