0

我已经创建了一个服务器,并且希望对其进行扩展,以便它只理解将从浏览器发送的指定 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();
        }
    }
}
}
4

1 回答 1

0

这是一个对 HTTP 协议进行了很好总结的页面(尽管您可以随意搜索和查找其他协议):http ://www.jmarshall.com/easy/http/ 。基本上,http 只是将文本流发送到套接字并接收响应。请求以“GET”开头(例如“GET /some/path HTTP/1.0”)。可能有也可能没有额外的标题和数据。响应类似“”,例如“HTTP/1.0 200 OK”,后跟标头和数据(包括内容)。

这是一个可以实现的项目,因为 HTTP 是一个简单的请求/响应协议。

如果您真的在询问如何从套接字读取/写入,那么这是从套接字(API 的 javadoc)简单读取,直到您到达请求的末尾,然后发送响应。

于 2013-11-14T18:16:07.743 回答