设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 356|回复: 0

Post to Website using Jakarta Commons HTTP Client

[复制链接]
发表于 2013-5-17 13:42:54 | 显示全部楼层 |阅读模式
  1. package net.alsee.hotfolder.processors;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;

  5. import net.alsee.hotfolder.core.context.IHotFolderContext;
  6. import net.alsee.hotfolder.processors.IHotFolderProcessor;

  7. import org.apache.commons.httpclient.HttpClient;
  8. import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
  9. import org.apache.commons.httpclient.methods.PostMethod;
  10. import org.apache.log4j.Logger;

  11. /**
  12. * Processes a HTTP Post Request with the file in the HotFolder Requires:
  13. * Jakarta Commons HTTP Client (http://jakarta.apache.org/commons/httpclient/)
  14. *
  15. * @author R. Albisser
  16. */
  17. public class HTTPPostProcessor implements IHotFolderProcessor {
  18.     private static final String DEFAULT_CONTENT_TYPE = "text/xml; charset=ISO-8859-1";

  19.     private static final Logger log = Logger.getLogger(HTTPPostProcessor.class);

  20.     private IHotFolderContext ctx;

  21.     /**
  22.      * @see net.alsee.hotfolder.processors.IHotFolderProcessor#fileProcess(java.io.File,
  23.      *      java.lang.String)
  24.      */
  25.     public void fileProcess(File file, String id) {
  26.         log.info("file: " + file.getName() + " [" + file.getAbsoluteFile()
  27.                 + "] process!");
  28.         process(file);
  29.     }

  30.     /**
  31.      * @see net.alsee.hotfolder.processors.IHotFolderProcessor#fileFound(java.io.File,
  32.      *      java.lang.String)
  33.      */
  34.     public void fileFound(File file, String id) {
  35.         log.debug("file: " + file.getName() + " [" + file.getAbsoluteFile()
  36.                 + "] found!");
  37.     }

  38.     /**
  39.      * Method process.
  40.      *
  41.      * @param file
  42.      *            File
  43.      */
  44.     private void process(File file) {
  45.         processHTTPPostRequest(file);
  46.     }

  47.     private String processHTTPPostRequest(File file) {
  48.         String url = ctx.getProperty("parameters.url");
  49.         // url =
  50.         // "http://vies1nyx.sie.siemens.at:8034/sap/xi/adapter_plain?namespace=http%3A//siemens.com/SIE/SIBT/xi/EUROPE/Quotation&interface=Sender_Quotation_IT&service=BS_QS_BE_QA&QOS=EO&sap-user=XIAPPLUSER1&sap-password=APPXI$1&sap-client=710";
  51.         if (url == null || url.length() < 1) {
  52.             log.warn("processHTTPPostRequest: Url invalid (" + url + ")");
  53.         }
  54.         String contentType = ctx.getProperty("parameters.contentType");
  55.         if (contentType == null || contentType.length() < 1) {
  56.             contentType = DEFAULT_CONTENT_TYPE;
  57.         }
  58.         String timeOut = ctx.getProperty("parameters.timeout");
  59.         String responseCode = "";
  60.         File postFile = new File(file.getAbsolutePath());
  61.         PostMethod post = new PostMethod(url);
  62.         HttpClient httpclient;
  63.         FileInputStream fis = null;
  64.         try {
  65.             fis = new FileInputStream(postFile);
  66.             post.setRequestEntity(new InputStreamRequestEntity(fis, postFile
  67.                     .length()));
  68.             post.setRequestHeader("Content-type", contentType);
  69.             httpclient = new HttpClient();
  70.             if (timeOut != null) {
  71.                 try {
  72.                     int timeOutInt = Integer.parseInt(timeOut);
  73.                     if (timeOutInt > 1000) {
  74.                         log.info("Set Timeout to: " + timeOutInt);
  75.                         httpclient.setConnectionTimeout(timeOutInt);
  76.                     }
  77.                 } catch (Exception ignore) {
  78.                 }
  79.             }
  80.             log.info("Execute HTTPPost: " + url);
  81.             int result = httpclient.executeMethod(post);
  82.             log.info("Response status code: " + result);
  83.             log.debug("Response body: " + post.getResponseBodyAsString());
  84.             responseCode = Integer.toString(result);
  85.             // try {
  86.             // Thread.sleep(20000);
  87.             // } catch (InterruptedException ignore) {
  88.             // ignore.printStackTrace();
  89.             // }
  90.         } catch (Exception e) {
  91.             log.warn("Response status code: " + responseCode + " " + e);
  92.             e.printStackTrace();
  93.         } finally {
  94.             try {
  95.                 if ((responseCode == null) || (!responseCode.equals("200"))) {
  96.                     String msg = "ResponseCode: " + responseCode + "\nUrl: "
  97.                             + url + "\nFile: " + file.getAbsolutePath();
  98.                     log.warn(msg);
  99.                     ctx.sendNotificationEmail("HTTPPostProcessor Status: "
  100.                             + responseCode + "", msg);
  101.                 }
  102.                 // Release current connection to the connection pool once you
  103.                 // are
  104.                 // done
  105.                 try {
  106.                     fis.close();
  107.                 } catch (IOException e) {
  108.                     // e.printStackTrace();
  109.                 }
  110.                 post.releaseConnection();
  111.                 post = null;
  112.                 httpclient = null;
  113.             } catch (Exception ignore) {
  114.                 // ignore
  115.             }
  116.         }
  117.         return responseCode;
  118.     }

  119.     /**
  120.      * @see net.alsee.hotfolder.processors.IHotFolderProcessor#setProcessorContext(net.alsee.hotfolder.core.context.IHotFolderContext)
  121.      */
  122.     public void setProcessorContext(IHotFolderContext procContext) {
  123.         this.ctx = procContext;
  124.     }
  125. }
复制代码
This sample is from HotFolder project.

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 18:53 , Processed in 0.011587 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表