Java Exception- " Server returned HTTP response code: 401 for URL..."

Posted on 2008-11-03
Last Modified: 2013-11-22
Hi experts,
I have encountered with a problem. I have given my code below. When I run that code, I get the following exception

>> Server returned HTTP response code: 401 for URL:
      at Source)
      at Source)
      at GetData.readBuildRequest(
      at GetData.sendRequestString(
      at GetData.main(

When I access it by giving appropriate username and pasword in browser, it works( But not with this java code. I know this is related to some authentication problem. But how to overcome this?
Any help in this regard will be well appreciated with points.
Warm Regards,

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 + "";
		System.out.println("This is the csvString"+csvString);
		System.out.println("chaine non encodee : " + csvString);
		return csvString;	
	public String setEncodedCSVString(String csvString)
		String encodedCsvString="";
			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 =null;
		return RecupFileFinal;
	public static HttpsURLConnection sendRequest(String stringURL,String data)
		HttpsURLConnection connection=null ;
		// Creation de la connection
			//String def = URLEncoder.encode(data);
			//"def : " + def);
		URL url = new URL(stringURL);
		connection = (HttpsURLConnection)url.openConnection();
	//	URLConnection connection = url.openConnection();
		//envoi de la requête
		OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
		catch(MalformedURLException mue)
		catch(IOException ioe){ ioe.printStackTrace(); }
		return connection;
	public static String readBuildRequest(HttpsURLConnection connection)
		String line;
		String fileRequest=null;
		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"; 

Open in new window

Question by:Sreejith22
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

Accepted Solution

CEHJ earned 300 total points
ID: 22866389
Try doing the authentication like this:
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 200 total points
ID: 22866452
You are correct it is authentication related as the 401 status code is the UNATHORIZED status for

What you may need to do is pass these values in header versus in URL.  Let me see if I can find an example.
LVL 60

Expert Comment

by:Kevin Cross
ID: 22866458
Actually that is a good one from CEHJ, I just didn't have my browser refreshed yet.  
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 22869713
Thanks CEHJ and mwvisa1,
I got the output with

Still, thanks a lot for your valuable help.

LVL 60

Expert Comment

by:Kevin Cross
ID: 22870298
Glad you found that.  CEHJ posted that link to answer another question along this lines...  Just glad to share in his genius on this occassion. :)
LVL 86

Expert Comment

ID: 22871136

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
As a financial services provider, your business is impacted by two of the strictest federal regulations on record: the Sarbanes-Oxley Act and the Gramm-Leach-Bliley Act. Correctly implementing faxing into your organization to provide secure, real-ti…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question