我一直收到这个错误,我真的不知道为什么。我尝试过不同的网址但我仍然遇到同样的错误。
java.net.MalformedURLException java.net.URL。(未知来源)
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();
}
}
}
任何帮助都将不胜感激!!
谢谢!
答案 0 :(得分:1)
此代码无法编译。
您的网址无效。不能假设它是如上所示。
此外,您必须URLEncode参数名称和值。