Jeg har et par eksempler liggende:
import java.net.*;
import java.io.*;
public class HttpGet {
public static void main(String[] args) {
try {
URL url = new URL("
http://www.domain.dk/htbin/tell2?nam=val");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.connect();
if(con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = con.getInputStream();
byte[] b = new byte[1000];
int n;
while((n = is.read(b)) >= 0) {
System.out.println(new String(b,0,n));
}
is.close();
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class HttpPost {
public static void main(String[] args) {
try {
URL url = new URL("
http://www.domain.dk/htbin/tell2");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
String info = "nam=val";
con.setDoOutput(true);
con.getOutputStream().write(info.getBytes());
con.connect();
if(con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = con.getInputStream();
byte[] b = new byte[1000];
int n;
while((n = is.read(b)) >= 0) {
System.out.println(new String(b,0,n));
}
is.close();
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}