Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

"java.io.IOException: Server returned HTTP response code: 401 for URL..."

aneesahamedaaNov 3 2008 — edited Nov 23 2008
Hi experts,
I have encountered with a problem. I have given my code below. When I run that code, I get the following exception
java.io.IOException: Server returned HTTP response code: 401 for URL: https://LOGIN:PASSWORD@www.mybooo.com/core/Dd002wW.php
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
	at GetData.readBuildRequest(GetData.java:98)
	at GetData.sendRequestString(GetData.java:48)
	at GetData.main(GetData.java:118)
My code goes here..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;


public class GetData
{
	public static final String UTF8_ENCODING = "UTF-8";
	public static String username;
	public static String password;
	private static String urlCheck = "";
	private static final String POST = "POST";
	public String setCSVString(String username, String password)
	{
		String csvString = "";
		//csvString = "{usr:'" + username + "',pwd:'"+password+"'}";
		urlCheck = "https://" + username +":"+ password + "@www.mybooo.com/core/Dd002wW.php";
		System.out.println("This is the csvString"+csvString);
		System.out.println("chaine non encodee : " + csvString);
		return csvString;	
	}

	public String setEncodedCSVString(String csvString)
	{
		String encodedCsvString="";
		//try 
		{
			encodedCsvString = URLEncoder.encode("data={action:'contacts',args:''}");// + URLEncoder.encode(csvString, UTF8_ENCODING);
			System.out.println("chaine encodee : " + encodedCsvString);
		} 
		//catch (UnsupportedEncodingException e) { e.printStackTrace();}
		
		return encodedCsvString;
		
	}
	public static String sendRequestString(String encodedCsvString)
	{
		HttpsURLConnection connection = null;
		connection = sendRequest(urlCheck,encodedCsvString);
		
		// Reponse du serveur
		String RecupFile = readBuildRequest(connection);
		
		System.out.println("recupFile : " + RecupFile);
		String recupFileSub = RecupFile.substring(RecupFile.indexOf("'")+1, RecupFile.lastIndexOf("'"));
		//String RecupFileFinal = recupFileSub.replaceAll("\"", "");
		String RecupFileFinal = recupFileSub;
		connection.disconnect();
		connection =null;
		return RecupFileFinal;
	}
	public static HttpsURLConnection sendRequest(String stringURL,String data)
	{
		HttpsURLConnection connection=null ;
		// Creation de la connection
		try{

			//String def = URLEncoder.encode(data);
			//log.info("def : " + def);
		URL url = new URL(stringURL);
		connection = (HttpsURLConnection)url.openConnection();

	//	URLConnection connection = url.openConnection();
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setRequestMethod(POST);
		
		//envoi de la requête
		OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

		writer.write(data);
		writer.flush();
		writer.close();
		
		}
		catch(MalformedURLException mue)
		{ 
			mue.printStackTrace(); 
		}
		catch(IOException ioe){ ioe.printStackTrace(); }
		return connection;
	}
	public static String readBuildRequest(HttpsURLConnection connection)
	{

		String line;
		String fileRequest=null;
		try
		{
		BufferedReader reader = null;
		//lecture de la réponse
		reader =  new BufferedReader(new InputStreamReader(connection.getInputStream()));

		//FileHelper fileHelper = new FileHelper();
			while ((line = reader.readLine()) != null) 
			{
				System.out.println("reader " + line);
				fileRequest =""+ line;
				System.out.println("filerequest"+ fileRequest);
			}	

		}
		catch(MalformedURLException mue) { mue.printStackTrace(); }
		catch(IOException ioe) { ioe.printStackTrace(); }
		
		System.out.println("filerequest"+ fileRequest);
		return fileRequest;
	}
	public static void main(String args[]){
		GetData ut = new GetData();
		final	String currRep = "/"+"src.rar"; 
		System.out.println(ut.sendRequestString(ut.setEncodedCSVString(ut.setCSVString("LOGIN","PASSWORD"))));
		//System.out.println(System.getProperties().getProperty("java.home"));
	}
}
{code}

When I access it by giving appropriate username and pasword in browser, it works(https://LOGIN:PASSWORD@mybooo.com/core/Dd002wW.php).. But not with this java code. I know this is related to some authentication problem. But how to overcome this?
Any help in this egard will be well appreciated with dukes.
Warm Regards,
Anees                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 21 2008
Added on Nov 3 2008
2 comments
1,562 views