blogspot.com-GA4

星期二, 8月 09, 2011

servlet通訊協定溝通


URL url = new URL("http://127.0.0.1:8080/cts/CreateToken.do?method=view");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Length", "" + sb.length());

// 當有server 無法進行存取設定時,增加以下兩行設定
connection.setDoInput(true);
//connection.setDoOutput(true);

// 方法一
OutputStreamWriter outputWriter = new OutputStreamWriter( connection.getOutputStream());
outputWriter.write(sb.toString());
outputWriter.flush();
outputWriter.close();

// 方法二
DataInputStream in = new DataInputStream(connection.getInputStream());
String line;
while((line = in.readLine())!=null){
System.out.println(line);
}
in.close();