gpt4 book ai didi

java - 从 nanoHttpd 服务器重定向到另一个域

转载 作者:行者123 更新时间:2023-11-30 06:09:23 25 4
gpt4 key购买 nike

我已经实现了 nanohttpd 服务器 nano我的目标是根据我的条件将请求转发到不同的域。

我的代码是这样的

package CreateServer;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import fi.iki.elonen.NanoHTTPD;

import javax.xml.ws.Response;

public class App extends NanoHTTPD {

public App() throws IOException {
super(8080);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
System.out.println("\nRunning! Point your browers to http://localhost:8080/ \n");

}

public static void main(String[] args) {
try {
new App();
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
}
}

@Override
public Response serve(IHTTPSession session) {
String msg = "<html><body><h1>Hello server</h1>\n";
Map<String, String> parms = session.getParms();
if (parms.get("username") == null) {
msg += "<form action='?' method='get'>\n <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
} else {
msg += "<p>Hello, " + parms.get("username") + "!</p>";
}
String websiteName="https://www.google.com";
StringBuilder html=new StringBuilder();
html.append("<html><head><meta http-equiv=\"refresh\" content=\"0; URL='"+websiteName+"'\" /></head>");
html.append("<body></body></html>\n");

// return new Response(Response.Status.OK, MIME_PLAINTEXT, null, 0);
return newFixedLengthResponse(msg + "</body></html>\n");
// return newFixedLengthResponse(html.toString());
// Response response=new Response(Response.IStatus.class.);
// response.sendRedirect("login.jsp");

//return Response()
}

}

问题是每当我尝试重定向到另一个域时

它重定向到 https://www.google.com

但它从客户端重定向到 www.google.com。但是除了客户端之外,是否有任何正确的方法可以将请求发送到服务器端?????

我该怎么做?他们有其他方法吗?请帮忙。

最佳答案

在下面的示例中,当用户尝试打开您的 site.com/redirectme 时,它​​将被重定向到 google。

@Override
public Response serve(IHTTPSession session) {

switch (session.getUri()) {

case "/redirectme":

Response r = newFixedLengthResponse(Response.Status.REDIRECT, MIME_HTML, "");
r.addHeader("Location", "http://google.com");
return r;
default:
return super.serve(session);
}

关于java - 从 nanoHttpd 服务器重定向到另一个域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38058019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com