F
Flip
I'm trying to post data to a webpage, then get the next page, but I can't
seem to get that to work. I'm trying to follow examples on a couple of
webpages, but I'm always getting back the same html as I started with. Can
anyone see what I'm doing wrong? Thanks.
Code snippet-------------------------------------------------------------
package com.pch.serverconnection;
import java.net.*;
import java.io.*;
public class GoodURLPost {
public GoodURLPost() {
try {
StringBuffer sb = new StringBuffer();
URL url = new URL( "http://www.domainnamehere.com/default.jsp" );
String body = "/servlet/DoSearch?aSearchString=searchforsomestring";
// find the newline character(s) on the current system
String newline = null;
try {
newline = System.getProperty("line.separator");
} catch (Exception e) {
newline = "\n";
}
// URL must use the http protocol!
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(false); // you may not ask the user
conn.setDoOutput(true); // we want to send things
// the Content-type should be default, but we set it anyway
//conn.setRequestProperty( "Content-type", "text/html" );
// the content-length should not be necessary, but we're cautious
conn.setRequestProperty( "Content-length",
Integer.toString(body.length()));
// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();
PrintWriter pw = new PrintWriter(rawOutStream);
pw.print(body); // here we "send" our body!
pw.flush();
pw.close();
// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you get the
// InputStream before completely writing out your output first!
InputStream rawInStream = conn.getInputStream();
// get response
BufferedReader rdr = new BufferedReader(new
InputStreamReader(rawInStream));
String line;
while ((line = rdr.readLine()) != null) {
sb.append(line + newline );
}
System.out.println( sb.toString() );
} catch (Exception e) {
System.out.println("Exception "+e.toString());
e.printStackTrace();
}
}
public static void main(String[] args) {
GoodURLPost goodURLPost1 = new GoodURLPost();
}
}
seem to get that to work. I'm trying to follow examples on a couple of
webpages, but I'm always getting back the same html as I started with. Can
anyone see what I'm doing wrong? Thanks.
Code snippet-------------------------------------------------------------
package com.pch.serverconnection;
import java.net.*;
import java.io.*;
public class GoodURLPost {
public GoodURLPost() {
try {
StringBuffer sb = new StringBuffer();
URL url = new URL( "http://www.domainnamehere.com/default.jsp" );
String body = "/servlet/DoSearch?aSearchString=searchforsomestring";
// find the newline character(s) on the current system
String newline = null;
try {
newline = System.getProperty("line.separator");
} catch (Exception e) {
newline = "\n";
}
// URL must use the http protocol!
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(false); // you may not ask the user
conn.setDoOutput(true); // we want to send things
// the Content-type should be default, but we set it anyway
//conn.setRequestProperty( "Content-type", "text/html" );
// the content-length should not be necessary, but we're cautious
conn.setRequestProperty( "Content-length",
Integer.toString(body.length()));
// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();
PrintWriter pw = new PrintWriter(rawOutStream);
pw.print(body); // here we "send" our body!
pw.flush();
pw.close();
// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you get the
// InputStream before completely writing out your output first!
InputStream rawInStream = conn.getInputStream();
// get response
BufferedReader rdr = new BufferedReader(new
InputStreamReader(rawInStream));
String line;
while ((line = rdr.readLine()) != null) {
sb.append(line + newline );
}
System.out.println( sb.toString() );
} catch (Exception e) {
System.out.println("Exception "+e.toString());
e.printStackTrace();
}
}
public static void main(String[] args) {
GoodURLPost goodURLPost1 = new GoodURLPost();
}
}