通过提供凭据使用Android应用程序登录到Web服务

时间:2014-04-21 11:00:43

标签: java android login client basic-authentication

浏览器上打开的Web服务会显示一个弹出框,询问用户名和密码,如何通过代码提供用户名和密码来登录和检索html代码。

请建议一种如何提供凭据的方法。

继承我的代码

package com.test.connection;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.catalina.connector.InputBuffer;
import org.apache.http.client.HttpClient;

 public class ReadWebSiteHtml {


public static void main(String[] args) throws IOException {

    final String username="name";
    final String password="pass";
       URL url = new URL("https:/myservice/today"); //on entering google.com it retrives the html code from google.com since it does not require username and password, but how do i supply credentials to my web service?. 

    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password.toCharArray());
        }
    });

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

    try {
        BufferedReader in = 
                new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        /*  InputStream in = new BufferedInputStream(urlConnection.getInputStream());*/
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        WriteToFile(response);
        System.out.println(response);
        in.close();
    }
    finally {
        urlConnection.disconnect();
    }
}


public static void WriteToFile(StringBuffer response){
    try {
File file = new File("d://test//writtenfile//htmlFile.html");

        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(response.toString());
        bw.close();

        System.out.println("Done writing the Html file!");

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

1 个答案:

答案 0 :(得分:0)

您可以使用它将数据发送到webservice

public static final String SOAP_ACTION = "http://xxxxxx.com/mapsupport/RegisterOnline";
public static final String OPERATION_NAME = "RegisterOnline";// your webservice web method name     
public static final String WSDL_TARGET_NAMESPACE = "http://xxxxxx.com/mapsupport/";
public static final String SOAP_ADDRESS = "http://xxxxxx.com/MapSupport.asmx";

PropertyInfo userid,password;

在onCreate()

userid=new PropertyInfo();
password=new PropertyInfo();

request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
httpTransport = new HttpTransportSE(SOAP_ADDRESS);

userid.setName("userid");
userid.setValue("youruserid");
userid.setType(String.class);
password.setName("password");
password.setValue("Yourpassword");
password.setType(String.class);

request.addProperty(userid);
request.addProperty(password);

httpTransport.call(SOAP_ACTION, envelope); 
response = (SoapPrimitive) envelope.getResponse();
resp=response.toString();

here

下载ksoap2 jar文件