【发布时间】: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