转载于:https://blog.csdn.net/yongwoozzang/article/details/83029128
public static String Post(String httpUrl, String data) {
PrintWriter out = null;
InputStream readStream = null;
String output = data;
String result = "";
try {
output = "value1=" + URLEncoder.encode(output, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
URL postUrl = new URL(httpUrl);
HttpURLConnection conn = (HttpURLConnection) postUrl.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");//关键代码 application/x-www-form-urlencoded
out = new PrintWriter(conn.getOutputStream());
out.print(output);
out.flush();
out.close();
if (conn.getResponseCode() != 200) {
throw new Exception("请求url失败");
}
readStream = conn.getInputStream();
result = ll.InputStreamToString(readStream);
return result;
} catch (Exception e) {
e.printStackTrace();
result = ll.InputStreamToString(readStream);
return result;
} finally {
if (out != null) {
out.close();
}
if (readStream != null) {
try {
readStream.close();
} catch (IOException e) {
return null;
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185280.html原文链接:https://javaforall.cn