我已经创建了一个服务器,并且希望对其进行扩展,以便它只理解将从浏览器发送的指定 HTTP GET 命令并将响应发送到 get 命令;它不必有什么特别的,必须在 java 1.6 上完成。
我不确定如何实现这一点,甚至不确定如何发送 GET 命令。 我的问题是:我如何发送和读取 GET 命令以及如何使用套接字响应我,而不是运行时。
package server;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.*;
import blogs.Logs;
import blogs.ServerMessages;
import logins.StreamUP;
public class TwipperServer implements Runnable {
Socket csocket;
TwipperServer(Socket csocket) {
this.csocket = csocket;
}
Logs serverLogs = new Logs();
PrintWriter out = null;
BufferedReader in = null;
boolean loginBool = false;
String tempUser = null;
String tempPass = null;
StreamUP strm = new StreamUP();
boolean userLogout = false;
String strMessage = "Enter Username";
List<ServerMessages> postedMessages = new ArrayList<ServerMessages>();
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(new TwipperServer(clientSocket)).start();
}
}
@SuppressWarnings("deprecation")
private void chooseMethod(int num) throws IOException {
String inputLine;
switch (num) {
case 1:// add status
out.println("Enter status");
inputLine = in.readLine();
serverLogs.addMessage(tempUser, new Date(), inputLine);
serverLogs.updateLogs();
break;
case 2:// remove status by specific date and hour
int year,
month,
date,
hrs,
min = 0;
out.println("Enter year");
year = Integer.parseInt(in.readLine());
out.println("Enter month");
month = Integer.parseInt(in.readLine());
out.println("Enter day");
date = Integer.parseInt(in.readLine());
out.println("Enter hour");
hrs = Integer.parseInt(in.readLine());
Date de = new Date(year - 1900, month - 1, date, hrs, min);
List<ServerMessages> removeLogs = new ArrayList<ServerMessages>();
for (ServerMessages temp : serverLogs.messageLogs) {
if (temp.getUser().equals(tempUser)) {
if (temp.getDate().getYear() == de.getYear()
&& temp.getDate().getMonth() == de.getMonth()
&& temp.getDate().getDay() == de.getDay()) {
int hour = de.getHours();
int compare = temp.getDate().getHours();
if (compare == hour)
removeLogs.add(temp);
}
}
}
serverLogs.messageLogs.removeAll(removeLogs);
serverLogs.updateLogs();
break;
case 3:// show messages containing specific hashtag
String hashTag;
out.println("Enter hashtag you wish to search");
hashTag = in.readLine();
for (ServerMessages temp : serverLogs.messageLogs) {
temp.getHash(hashTag);
}
break;
case 4:
for (ServerMessages n : serverLogs.messageLogs) {
System.out.println(n.getData());
}
break;
default:
break;
}
}
@Override
public void run() {
try {
out = new PrintWriter(csocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
csocket.getInputStream()));
String inputLine;
int inputCom;
while (userLogout == false) {
while (loginBool == false) {// how to send html password request
out.println(strMessage);// for username and password
tempUser = in.readLine();
out.println("Enter password: ");
tempPass = in.readLine();
loginBool = strm.validity(tempUser, tempPass);
strMessage = "Incorrect username or password. Please try again. Enter Username: ";
}// end while loop for login check
strMessage = "Press 1 to add message";
out.println(strMessage);
inputLine = in.readLine();
inputCom = Integer.parseInt(inputLine);
try {
chooseMethod(inputCom);// what to do
} catch (Exception e) {
e.printStackTrace();
}
}// end while loop for userlogout
} catch (IOException e) {
System.out
.println("Exception caught when trying to listen on port "
+ 9999 + " or listening for a connection");
System.out.println(e.getMessage());
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
out.close();
try {
csocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}