POSTing via Java

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 John D. Mitchell



Advertisement
<script language=JavaScript src="http://spinbox.macworld.com/?DC=jw-BigBox&JS=Y&TARGET=_top&BA=1"></script> [被屏蔽广告]

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!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POSTING_INTERFACE_CLEARING是一个函数,用于进行标准清账操作。它封装了BDC录屏的功能。\[3\]在使用该函数时,如果传入参数ITEM,则必须传入公司代码、记账码和科目。根据你提供的引用内容,你的问题是关于批量创建清账凭证时参照码3的问题。你希望每个参照码3对应一张凭证,但实际上清账凭证创建成功后,参照码3一直沿用了模板中的第一个值。你进行了排查并确认了传参数据的正确性,并清空了相应的内表。然后你尝试使用了free memory来清空ABAP内存变量,并成功解决了问题。然而,你想知道使用free memory会有什么影响。 #### 引用[.reference_title] - *1* [SAP-ABAP-清账函数BAPI使用POSTING_INTERFACE_CLEARING,应付暂估外币清账程序](https://blog.csdn.net/Qunending/article/details/124666847)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [清账函数POSTING_INTERFACE_CLEARING的应用](https://blog.csdn.net/wangshaoqiang022/article/details/90234375)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值