sendget.java
来自「Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用」· Java 代码 · 共 95 行
JAVA
95 行
import java.net.*;
import java.io.*;
// Chapter 9, Listing 6
public class SendGET
{
public static void main(String args[]) throws IOException
{
// Check command line parameters
if (args.length < 1)
{
System.out.println ("Syntax- SendGET baseurl");
System.in.read();
return;
}
// Get the base URL of the cgi-script/servlet
String baseURL = args[0];
// Start with a ? for the query string
String arguments = "?";
// Create a buffered reader, for reading CGI
// parameters from the user
BufferedReader reader = new BufferedReader (
new InputStreamReader ( System.in )
);
// Loop until no parameters left
for (;;)
{
System.out.println ("Enter field ( . terminates )");
String field = reader.readLine();
// If a . char entered, terminate loop
if (field.equals ("."))
break;
System.out.println ("Enter value");
String value = reader.readLine();
// Encode the URL value
arguments += URLEncoder.encode(field)
+ "=" + URLEncoder.encode(value) + "&";
}
// Construct the full GET request
String finalURL = baseURL + arguments;
System.out.println ("Sending GET request - " + finalURL);
// Send the GET request, and display output
try
{
// Construct the url object
URL url = new URL(finalURL);
// Open a connection
InputStream input = url.openStream();
// Buffer the stream, for better performance
BufferedInputStream bufIn = new BufferedInputStream(input);
// Repeat until end of file
for (;;)
{
int data = bufIn.read();
// Check for EOF
if (data == -1)
break;
else
System.out.print ( (char) data);
}
// Pause for user
System.out.println ();
System.out.println ("Hit enter to continue");
System.in.read();
}
catch (MalformedURLException mue)
{
System.err.println ("Bad URL - " + finalURL);
}
catch (IOException ioe)
{
System.err.println ("I/O error " + ioe);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?