Meaningless Drivel is fun!*
The moose likes Sockets and Internet Protocols and the fly likes HttpURLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "HttpURLConnection" Watch "HttpURLConnection" New topic
Author

HttpURLConnection

Raja Islam
Ranch Hand

Joined: Dec 07, 2001
Posts: 74
Hi
i want to submit a simple form using threading.
import java.net.*;
import java.io.*;
public class Untitled1
{
public static void main(String args[])
{
try
{
URL cellUrl = new URL("http://127.0.0.1:8080/a/servlet/CreateFile");
HttpURLConnection con = (HttpURLConnection) cellUrl.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");

String x = "Hi HTTP SERVER! Just a quick hello!";
con.setRequestProperty("CONTENT_LENGTH", "" + x.length());
System.out.println("Msg Length: " + x.length());
System.out.println("Getting an output stream...");
OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(x);
osw.flush();
osw.close();
System.out.println("After flushing output stream. ");
InputStream is = con.getInputStream();
// any response?

/* InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
{
System.out.println("line: " + line);
}*/
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

the output is
Msg Length: 35
Getting an output stream...
After flushing output stream.
java.io.FileNotFoundException: http://127.0.0.1:8080/a/servlet/CreateFile
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:574)
at untitled1.Untitled1.main(Untitled1.java:32)
and the code CreateFile servlet is
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CreateFile extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res) {
try {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<form method=post>");
out.println("<input type=text name=x>");
out.println("<input type=submit>");
out.println("</form>");
}
catch(IOException ex){System.out.println("Error "+ex);}
}
public void doPost(HttpServletRequest req,HttpServletResponse res) {
try{ System.out.println("Creating File");
FileOutputStream file=new FileOutputStream("String.txt");
DataOutputStream dos=new DataOutputStream(file);
dos.writeBytes(req.getParameter("x"));
file.close();
}
catch(IOException ex){System.out.println("Error "+ex);}
}
}

i am using this code but it is not working. can any one explain what is problem with my code.it create file but throws and exception
java.io.FileNotFoundException: http://127.0.0.1:8080/a/servlet/CreateFile
i want to get data from Untitled1 as a parameter String.
Raja Islam
Ranch Hand

Joined: Dec 07, 2001
Posts: 74
Hi
I solved my prob it is due to cause i am not passing parameter value to CreateFile Servlet.
 
Consider Paul's rocket mass heater.
 
subject: HttpURLConnection
 
Similar Threads
Simple Problem
Simple Problem
HTTP Client/Server Problem
Form
HttpURLConnection