<span style="white-space:pre"> </span>try {
// 服务地址
URL url = new URL("http://192.168.1.1");
// 设定连接的相关参数
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8");
//配置提交参数
String rs = "user=123&pas=456";
out.write(rs);
out.flush();
out.close();
// 获取服务端的反馈
String strLine = "";
String strResponse = "";
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
while ((strLine = reader.readLine()) != null)
{
strResponse += strLine + "\n";
}
System.out.print(strResponse);
} catch (Exception e) {
System.out.println("异常信息:"+e.getMessage());
}
Java 模拟 Post提交
最新推荐文章于 2022-12-28 11:26:47 发布