求一个简单的JAVA里用HttpURLconnetcion连接Servelt的例子
答案:1 悬赏:70 手机版
解决时间 2021-02-19 07:10
- 提问者网友:沦陷
- 2021-02-19 00:59
求一个简单的JAVA里用HttpURLconnetcion连接Servelt的例子
最佳答案
- 五星知识达人网友:一袍清酒付
- 2021-02-19 02:38
public static String sendHttpPost(final String POST_URL, String postData) {
URL url = null;
OutputStreamWriter out = null;
StringBuffer xmlStr = new StringBuffer();
try {
url = new URL(POST_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", "" + postData.length());
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(postData.toString());
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "UTF-8"));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
xmlStr.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return xmlStr.toString();
}
URL url = null;
OutputStreamWriter out = null;
StringBuffer xmlStr = new StringBuffer();
try {
url = new URL(POST_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", "" + postData.length());
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(postData.toString());
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "UTF-8"));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
xmlStr.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return xmlStr.toString();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯