POSTing via Java

原创 2004年10月13日 13:19:00
Summary
This tip shows you how to send POST requests to Web servers and retrieve the response from an applet. (800 words) Please also see "Java Tip 41: POSTing Via Java Revisited" for more on this topic.
By

rule_ltblue.gif

Advertisement
[被屏蔽广告]?SIT=jw-BigBox&BA=1

The plethora of browsers in use turns the relatively simple act of performing POST-style HTTP requests into a bit of a pain. In this tip we shall provide the recipe for making POSTs simple again.

Everybody using the Web is at least passingly familiar with POST HTTP request. POST requests are used to send out things like HTML form data from a Web page to a Web server. A good example of a POST form is the feedback form at the bottom of this page. The browser sends the form data as part of the POST request, and the Web server sends back a response (which is usually in the form of another Web page).

The POST request, however, does not have to be used with an HTML form. It is just another HTTP request/response protocol. We can use it for whatever nefarious (or legitimate :-) deeds we care to. For instance, we can use POSTs to a CGI-bin script on a Web server rather than a raw socket to a database server. The advantage is you don't have to worry about some users not being able to use your applet because they happen to sit behind a firewall that refuses to allow random socket connections to the outside world.

You can see the results of invoking the Web server's CGI-bin script directly. Here is the Java POSTing applet in action:

The results of the Java applet should be here.

The code for dealing with posts is similar to the code for the Java application we developed for Java Tip #19 to copy files from Web sites. The core differences have to do with the security restrictions placed on applets.


    URL                 url;
    URLConnection urlConn;
    DataOutputStream    printout;
    DataInputStream     input;
    // URL of CGI-Bin script.
    url = new URL (getCodeBase().toString() + "env.tcgi");
    // URL connection channel.
    urlConn = url.openConnection();
    // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);
    // Let the RTS know that we want to do output.
    urlConn.setDoOutput (true);
    // No caching, we want the real thing.
    urlConn.setUseCaches (false);
    // Specify the content type.
    urlConn.setRequestProperty

("Content-Type", "application/x-www-form-urlencoded");
    // Send POST output.
    printout = new DataOutputStream (urlConn.getOutputStream ());
    String content =

"name=" + URLEncoder.encode ("Buford Early") +

"&email=" + URLEncoder.encode ("buford@known-space.com");
    printout.writeBytes (content);
    printout.flush ();
    printout.close ();
    // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());
    String str;
    while (null != ((str = input.readLine())))

{

System.out.println (str);

textArea.appendText (str + "/n");

}
    input.close ();

First we build the URL to the CGI-bin script on the server. We use getCodeBase() to give us the URL from which the applet was delivered. This is important since the (untrusted) applet is only allowed to contact the host from which it came.

After opening the URL connection, we have to tell the runtime system explicitly what we are going to be doing. We want to both send and receive data on this connection so we set both the input and output flags. We then turn off the use of caching since we want to make sure that the data we read back from the server is actually in response to the data that we send it. Finally, we set the HTTP request's "content-type" field to be "application/x-www-form-urlencoded." That is necessary since some version of Netscape's browser has a bug which does not give out this proper content type when doing POSTs.

Now, after chanting all of those necessary magical incantations, we can create and open an output stream for that URL connection. We then write the data we want POSTed to the cgi-bin script on the server. It is very important that we close the output stream immediately after we write the data. If we don't close the stream then the cgi-bin script will sit waiting for more data (and so will not yet be sending its response).

Once we have sent out all of the data, we then open up an input stream for the URL connection and read all of the server's response.

That's all there is to it! jw-dingbat.gif

 

POSTing via Java revisited

SummaryIn JavaWorlds August issue, we ran a Java Tip on "POSTing from applets." Not only was the tip...
  • nickeyfff
  • nickeyfff
  • 2004年10月13日 13:20
  • 954

Posting only possible in periods 2011/01 and 2010/12 in company code 1000

今天在做PP练习时,需要在库存中增加一些物料的库存数量,我用MB1C,Movement Type为561,系统提示:Posting only possible in periods 2011/01 a...
  • yika0101
  • yika0101
  • 2011年02月11日 16:01
  • 2909

How to generate an GL debug log file

Detail Log for GL programsUsed for concurrent request problems where we need to know what sql was us...
  • pan_tian
  • pan_tian
  • 2013年05月13日 16:41
  • 3420

Posting key in sap

记账码-posting key = PK 记账码有三个作用:1-代表借贷方 2-决定账户类型(S/D/K/M/A)3-决定屏幕字段状态 记帐码/Posting Key 在手工的会计核算过程中,...
  • jhzhou
  • jhzhou
  • 2012年09月02日 15:16
  • 1469

Posting only possible in periods 2002/11 and 2002/10 in company code 1000

在作PLM110的练习时,需要在库存中增加一些物料的库存数量,我用MB1C,movement type为561作了操作。 刚输入完相应的信息后,按回车,画面没变。 系统提示: ...
  • kehq2008
  • kehq2008
  • 2009年10月12日 23:07
  • 1814

SAP Review Stock Transfer posting

Stock Transfer& Transfer posting MB1B转储 MB5T->在途库存,不同工厂之间的转移 MB1B 313 发出动作 MB1C 315接受 Stock Tra...
  • Daniel_kong198577
  • Daniel_kong198577
  • 2013年08月22日 17:51
  • 793

关于Eclipse和STS里SVN报Subclipse talks to Subversion via a Java API that requires access to native错

Subclipse talks to Subversion via a Java API that requires access to native
  • zhang41228
  • zhang41228
  • 2016年09月19日 17:45
  • 4598

WM Posting change: Post to same bin

在处理WM转储记帐/记帐变更时,前台会有一个Post to same bin的建议。通常来讲,我们在做Posting change时,并不改变物料的物理位置或仓位,而只是作一些物料状态的变更,如QA库...
  • baiboyd
  • baiboyd
  • 2010年01月19日 16:43
  • 2672

跟日记帐录入相关的表

 跟日记帐录入相关的表 Journal Import Overview:3 G4 z( b+ /% p2 U( M( efl...
  • feixiangVB
  • feixiangVB
  • 2008年07月21日 14:40
  • 1503

关于Eclipse 64 bit里SVN报Subclipse talks to Subversion via a Java API that requires access to native错

打开eclipse  svn资源库后出错:Subclipse talks to Subversion via a Java API that requires access to native。 原...
  • gobravery
  • gobravery
  • 2017年05月24日 14:43
  • 834
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POSTing via Java
举报原因:
原因补充:

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