I keep getting this error and I really dont know why. I have tried different URLs but I still get the same error.
java.net.MalformedURLException
java.net.URL.(Unknown Source)
public class ProxyServlet extends HttpServlet {
private String PostUrl = "http://localhost:8080/myProxy/myServlet";
private static final long serialVersionUID = 1L;
public ProxyServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Parameters retreived...");
// map parameters to properties in config file and set response
Map();
}
private void Map() throws IOException{
String urlParameters = "topic="+topic+"&item="+item+"&period="+period+";
URL url = new URL(PostUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
System.out.println("Connection made to " + PostUrl);
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
OutputStream out = null;
try{
System.out.println("Writing data to output stream...");
out = conn.getOutputStream();
out.write(urlParameters.getBytes());
out.close();
System.out.println("All done!!!");
}
catch(IOException e){
e.printStackTrace();
}
}
}
Any help would be appreciated!!
Thanks!
This code doesn’t compile.
Your URL is invalid. Can’t assume it is as shown because of above.
Also you must URLEncode the parameter names and values.