How to use HttpURLConnection POST data to web server?
2012-10-26 12:42
441 查看
public static String excutePost(String targetURL, String urlParameters) { URL url; HttpURLConnection connection = null; try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); connection.setRequestProperty("Content-Language", "en-US"); connection.setUseCaches (false); connection.setDoInput(true); connection.setDoOutput(true); //Send request DataOutputStream wr = new DataOutputStream ( connection.getOutputStream ()); wr.writeBytes (urlParameters); wr.flush (); wr.close (); //Get Response InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); return null; } finally { if(connection != null) { connection.disconnect(); } } } ...
The urlParameters is a URL encoded string.
String urlParameters = "fName=" + URLEncoder.encode("???", "UTF-8") + "&lName=" + URLEncoder.encode("???", "UTF-8")
相关文章推荐
- How to use HttpURLConnection POST data to web server?
- How to use HttpWebRequest to post data to another page which is on another server
- How to use HttpWebRequest to send POST request to another web server?
- How to use HttpWebRequest to send POST request to another web server?
- How to use Trusted Connection when SQL server and web Server are on two separate machines.
- how to use UrlConnection to post/get request with basic authorization?
- httpurlconnection - How to use java.net.URLConnection to fire and handle HTTP requests? - Stack Overflow
- Use Http Post to call Web Service
- How to use GET and POST methods in HTTP from a MIDlet
- How to use HttpWebRequest and RegularExpressions to crash website
- How To Use Proxy Server To Access Internet at Shell Prompt With http_proxy Variable. [reprint]
- HttpURLConnection的post请求,什么时候发出,writeData存在什么地方
- How to retreive raw post data from HttpServletRequest in java
- Use Apache HttpClient to Post json data
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- How to retreive raw post data from HttpServletRequest in java
- How to retreive raw post data from HttpServletRequest in java
- How to use Cipher.exe to overwrite deleted data in Windows Server 2003
- How To Submit Form Data by Using XMLHTTP or ServerXMLHTTP Object
- How to retreive raw post data from HttpServletRequest in java