How to use HttpURLConnection POST data to web server?

转载 2016年06月01日 18:48:20
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 Turn Up Logging on the Siebel Web Server Extension in Siebel Versions 7.x and 8.x? [ID 477185

How To Turn Up Logging on the Siebel Web Server Extension in Siebel Versions 7.x and 8.x? [ID 477185...

How to monitor Nginx web server from the command line in real time

Once Nginx web server is up and running in a production environment, you will want to monitor its ...

How to Turn Your Mac Into a Web Server

From: http://www.macinstruct.com/node/112 How to Turn Your Mac Into a Web Server What You ...

SharePoint debug - Unable to render the data. If the problem persists, contact your web server admin

使用SharePoint Designer创建了一个外部内容类型external content type,连接SQL Server数据库中的一个表。
  • shrenk
  • shrenk
  • 2014-12-10 20:58
  • 1355

How to Install NoCat Splash and Link it to an External Web Server

Compile NoCatSplash: goto http://nocat.net/downloads/NoCatSplash/ and download the latest stable ...
  • zx824
  • zx824
  • 2012-07-09 11:11
  • 2944

How to Use Cocoa Bindings and Core Data in a Mac App

How to Use Cocoa Bindings and Core Data in a Mac App Learn how to use Cocoa...
  • yuanya
  • yuanya
  • 2014-05-05 13:36
  • 1081

How to use ISI Web of Knowledge

  • 2011-07-30 22:42
  • 9.80MB
  • 下载

how-to-configure-and-use-spark-history-server

how-to-configure-and-use-spark-history-server

Use Jquery to get json data from the server-side php page

Server-side: $bookmarkarray = array(); if(mysqli_connect_error()) { echo "failed to connect to t...

How to Post Data and Fetch Remote Pages from PHP Scripts

http://phpstarter.net/2008/12/how-to-post-data-and-fetch-remote-pages-from-php-scripts/ Published: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)