HTTP Request using JSP

In the last days I worked a lot around HTTP Request and Response stuff and here I would like to share some of the stuff.
The following code snippet shows a JSP page that sends an HTTP Request to a specified URL. It is also possible to specify custom headers (see myVeryCoolHeader) and it is also possible to send basix authentication for username/password secured websites:

<%@ page import="java.net.HttpURLConnection"%>
<%@ page import="java.net.URL"%>
<%@ page import="java.io.*" %>
<%
	
	URL myURL= new URL("http://mydummysite.org/testing/website.html");
	HttpURLConnection myconn = (HttpURLConnection) myURL.openConnection();

	String mybody = "This is the HTTP-Body. You can put here any text stuff you want";

	myconn.setRequestMethod("GET");
	myconn.setDoOutput(true);
	myconn.setUseCaches(false);
	myconn.setRequestProperty("myVeryCoolHeader", "My very cool header -value");
	
	
	// if the website requires basic authentication:
	boolean isSecuredWebsite = true;
	if (isSecuredWebsite){
		String login = "myUSER" + ":" + "myPassword";
		String loginEncoded = new sun.misc.BASE64Encoder().encode(login.getBytes());
		myconn.setRequestProperty("Authorization", "Basic " + loginEncoded);
	}
	
	// request the content as html text
	myconn.setRequestProperty("Content-Type", "text/html");
	myconn.setRequestProperty("Content-Length", String.valueOf(mybody.length()));
	
	// write the stuff to the output stream 
	OutputStreamWriter writer = new OutputStreamWriter(myconn.getOutputStream());
	writer.write("nn" + mybody);
	writer.flush();
	
	
	
	if(myconn.getResponseCode()== 200){
		// everyting ok, put the output from SAP into the HTTP Response
		BufferedReader reader = new BufferedReader(new InputStreamReader(myconn.getInputStream()));

		
		// write the stuff to the website
		String line = reader.readLine();
		while (line != null){
			out.println(line);
			line = reader.readLine();
		}
		reader.close();
		
	} else if(myconn.getResponseCode() == 401) {  
		// Catch the "not Authorized" Status code
		out.println("This Website is secured by Username and Password. <br>");
		
		out.println("<b>DEBUG-INFO</b>: <br>" +myconn.getHeaderField("WWW-Authenticate"));
		out.println("<br>URL: " + myURL.toString());
		out.println("<br>HTTP-Response Code: " + myconn.getResponseCode() + " " + myconn.getResponseMessage());


		
	} else{
		// all other errors like redirect, etc.
		out.println("An unexpected error appeared. Please inform your administrator about this issue. <br>");
		out.println("<b>DEBUG-INFO</b>: <br>");
		out.println("<br>URL: " + myURL.toString());
		out.println("<br>HTTP-Response Code: " + myconn.getResponseCode() + " " + myconn.getResponseMessage());
		
	}


	writer.close();
	myconn.disconnect();
	
%>