Question

the web service on opening on browser shows a popup box asking for username and password, how do i supply username and password via code to login and retrieve the html code.

please suggest a way to on how to supply credentials .

heres my code

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();
    }
}
 }
Was it helpful?

Solution

You can use this to send data to 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;

inside 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();

Download the ksoap2 jar files from here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top