我在一个java的client中使用
url = new URL(urlAddress);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
来将我一个文本框中的数据穿到一个servlet中,因为原来使用的是GET方法,所以直接在URL中写好参数就可以了。
可是现在要传的文本有2k所以只能改用post方法了。
找不到资料,请各位处理过这样的问题的给予指教。谢谢,在线急等
url = new URL(urlAddress);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
来将我一个文本框中的数据穿到一个servlet中,因为原来使用的是GET方法,所以直接在URL中写好参数就可以了。
可是现在要传的文本有2k所以只能改用post方法了。
找不到资料,请各位处理过这样的问题的给予指教。谢谢,在线急等
数据部分如下输出:
name=abc&password=797979也就是字段名加上等号再加上它的值,多个字段间以&分开,同名的属性的值以~`~分开(中间的字符是键盘上数字1左边的键,如name=abc~`~cde),数据输出完毕以两个换行符结束
注意:&"<>这四个符号出现在值中时要进行转换(同xml中的)
请指教
str=fdsalfdslafds这样在servlet中可以通过request.getParameter("str")来得到这个值
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
// Send data
URL url = new URL("http://hostname:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) {
}