try {
URL destURL = new URL("http://foo/cgi-bin/foo"); String requestString = "paramName=paramValue\r\n";
URLConnection urlConn = destURL.openConnection(); urlConn.setDoOutput(true); // we need to write
urlConn.setDoInput(true); // just to be safe...
urlConn.setUseCaches(false); // get info fresh from server// Tell the server what kind of data you are sending
urlConn.setRequestProperty("Content-type","application/octet-stream");// Must tell the server the size of the data you are sending.
// This also tells the URLConnection class that you are doing
// a POST instead of a GET.
urlConn.setRequestProperty("Content-length", ""+requestString.length());// Open an output stream so you can send the info you are posting
DataOutputStream outStream = new DataOutputStream(
urlConn.getOutputStream());// Write out the actual request data
outStream.writeBytes(requestString);
outStream.close();// Now that you have sent the data, open up an input stream and get
// the response back from the server
DataInputStream inStream = new DataInputStream(
urlConn.getInputStream()); int ch;
// Dump the contents of the request to System.out, or wherever
// you need , such String
while ((ch = inStream.read()) >= 0) {
System.out.print((char) ch);
} inStream.close(); } catch (Exception e) {
e.printStackTrace();
}
URL destURL = new URL("http://foo/cgi-bin/foo"); String requestString = "paramName=paramValue\r\n";
URLConnection urlConn = destURL.openConnection(); urlConn.setDoOutput(true); // we need to write
urlConn.setDoInput(true); // just to be safe...
urlConn.setUseCaches(false); // get info fresh from server// Tell the server what kind of data you are sending
urlConn.setRequestProperty("Content-type","application/octet-stream");// Must tell the server the size of the data you are sending.
// This also tells the URLConnection class that you are doing
// a POST instead of a GET.
urlConn.setRequestProperty("Content-length", ""+requestString.length());// Open an output stream so you can send the info you are posting
DataOutputStream outStream = new DataOutputStream(
urlConn.getOutputStream());// Write out the actual request data
outStream.writeBytes(requestString);
outStream.close();// Now that you have sent the data, open up an input stream and get
// the response back from the server
DataInputStream inStream = new DataInputStream(
urlConn.getInputStream()); int ch;
// Dump the contents of the request to System.out, or wherever
// you need , such String
while ((ch = inStream.read()) >= 0) {
System.out.print((char) ch);
} inStream.close(); } catch (Exception e) {
e.printStackTrace();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货