Http - Do a POST with HttpURLConnection
2017-02-09 00:00
260 查看
In a GET request, the parameters are sent as part of the URL.
In a POST request, the parameters are sent as a body of the request, after the headers.
To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.
This code should get you started:
If you want the result as a
参考:
http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily
http://hgoebl.github.io/DavidWebb/
In a POST request, the parameters are sent as a body of the request, after the headers.
To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.
This code should get you started:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.LinkedHashMap; import java.util.Map; public class Test { public static void main(String[] args) throws Exception { URL url = new URL("http://example.net/new-message.php"); Map<String, Object> params = new LinkedHashMap<>(); params.put("name", "Freddie the Fish"); params.put("email", "fishie@seamail.example.com"); params.put("reply_to_thread", 10394); params.put("message", "Shark attacks in Botany Bay have gotten out of control. We need more defensive dolphins to protect the schools here, but Mayor Porpoise is too busy stuffing his snout with lobsters. He's so shellfish."); StringBuilder postData = new StringBuilder(); for (Map.Entry<String, Object> param : params.entrySet()) { if (postData.length() != 0) postData.append('&'); postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); postData.append('='); postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); } byte[] postDataBytes = postData.toString().getBytes("UTF-8"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); conn.setDoOutput(true); conn.getOutputStream().write(postDataBytes); Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); for (int c; (c = in.read()) >= 0;) System.out.print((char) c); } }
If you want the result as a
Stringinstead of directly printed out do:
StringBuilder sb = new StringBuilder(); for (int c; (c = in.read()) >= 0;) sb.append((char)c); String response = sb.toString();
参考:
http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily
http://hgoebl.github.io/DavidWebb/
相关文章推荐
- Python使用post请求时遇到.ConnectionError: HTTPConnectionPool Max retries exceeded with url: Failed to esta
- 利用HttpURLConnection发送post请求上传文件
- 利用java的HttpURLConnection和Servlet通信,Post方式
- HttpURLConnection 的 POST 方法
- 使用HttpURLConnection向服务器发送post和get请求
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- Http学习之使用HttpURLConnection发送post和get请求
- Analyze URL Redirection with HTTP GET&POST
- Http学习之使用HttpURLConnection发送post请求深入
- 使用HttpURLConnection开发自动发送Get、Post请求并获取响应
- HttpURLConnection发送post和get请求
- 利用HttpURLConnection发送post请求上传多个文件
- Http学习之使用HttpURLConnection发送post和get请求
- Analyze URL Redirection with HTTP GET&POST
- Http学习之使用HttpURLConnection发送post和get请求
- HttpURLConnection发送post和get请求
- HttpURLConnection发送post和get请求
- 使用HttpURLConnection 的post方法来返回一个xml
- 使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection发送post和get请求