📄 sendget.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -