⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 posttest.java

📁 关于:Java2核心技术卷二 配套源码.
💻 JAVA
字号:
/**
 * @version 1.00 1999-08-28
 * @author Cay Horstmann
 */

import java.io.*;
import java.net.*;
import java.util.*;

public class PostTest
{  public static void main(String[] args)
   {  try
      {  String fileName;
         if (args.length > 0)
            fileName = args[0];
         else
            fileName = "PostTest.properties";
         Properties props = new Properties();
         FileInputStream in = new FileInputStream(fileName);
         props.load(in);

         URL url = new URL(props.getProperty("URL"));
         props.remove("URL");
         String r = doPost(url, props);
         System.out.println(r);
      }
      catch (IOException exception)
      {  System.out.println("Error: " + exception);
      }
   }

   public static String doPost(URL url,
      Properties nameValuePairs) throws IOException
   {  URLConnection connection = url.openConnection();
      connection.setDoOutput(true);

      PrintWriter out
         = new PrintWriter(connection.getOutputStream());

      Enumeration enum = nameValuePairs.keys();

      while (enum.hasMoreElements())
      {  String name = (String)enum.nextElement();
         String value = nameValuePairs.getProperty(name);
         char ch;
         if (enum.hasMoreElements()) ch = '&'; else ch = '\n';
         out.print(name + "="
            + URLEncoder.encode(value) + ch);
      }

      out.close();

      BufferedReader in;
      try
      {  in = new BufferedReader(new
            InputStreamReader(connection.getInputStream()));
      }
      catch (FileNotFoundException exception)
      {  InputStream err
            = ((HttpURLConnection)connection).getErrorStream();
         if (err == null) throw exception;
         in = new BufferedReader(new InputStreamReader(err));
      }
      StringBuffer response = new StringBuffer();
      String line;

      while ((line = in.readLine()) != null)
         response.append(line + "\n");

      in.close();
      return response.toString();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -