[java]通过读文件内容发起http请求

来源:互联网 发布:js解析unicode 编辑:程序博客网 时间:2024/05/12 14:26

public static void main(String[] args) throws HttpException, IOException {
  
   System.out.println("Response body: ");
   //File input = new File("d://selectProtocol.xml");
   File input = new File("d://insurance.xml");
  

    PostMethod post = new PostMethod("http://localhost:7001/bib-pafa-esb/remoting/pafaESBRequest");
      // 设置请求的内容直接从文件中读取
     FileInputStream fi = new FileInputStream(input);
    

      post.setRequestBody(new FileInputStream(input));

     

      if (input.length() < Integer.MAX_VALUE)

          post.setRequestContentLength(input.length());

      else            post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);

     

      // 指定请求内容的类型

      post.setRequestHeader("Content-type", "text/xml; charset=GBK");

     

      HttpClient httpclient = new HttpClient();

      int result = httpclient.executeMethod(post);

      System.out.println("Response status code: " + result);

      System.out.println("Response body: ");

      System.out.println(post.getResponseBodyAsString());

      post.releaseConnection();
 
 }
 

原创粉丝点击