0
我創建了一個服務器,我期待所以它只能理解將從瀏覽器發送,並會發送到獲得命令的響應指定HTTP GET命令將其展開;它不一定非常特別,必須在java 1.6上完成。HTTP服務器GET命令
我不能確定如何實現這種或如何,甚至發送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();
}
}
}
}
如果你已經有一個能理解HTTP協議的服務器,你可以試試[HTTP阿帕奇客戶端](http://hc.apache.org/)來編寫你的客戶。或者使用'java.net.URLConnection'類。您可能想了解REST,我認爲它可以幫助您構建您的應用程序。 PS:我的評論只是除了user1676075答案。另外,你可以試試你的瀏覽器:-) –