无法在命令提示符下使用 cURL 连接到本地主机

Cannot connect to localhost with cURL on Command Prompt

我刚刚在我的硬盘驱动器上安装了 cURL,以便使用我的本地主机进行测试(在命令提示符下)。但是我无法连接到任何端口。您可以在下面看到目录和我的输入:

C:\Users\me\curl\src>curl -v http://localhost:9000

这给了我:

  • Rebuilt URL to: http://localhost:9000/
  • timeout on name lookup is not supported
  • Trying ::1...
  • TCP_NODELAY set
  • Trying 127.0.0.1...
  • TCP_NODELAY set
  • connect to ::1 port 9000 failed: Connection refused
  • connect to 127.0.0.1 port 9000 failed: Connection refused
  • Failed to connect to localhost port 9000: Connection refused
  • Closing connection 0 curl: (7) Failed to connect to localhost port 9000: Connection refused

也许我应该补充一点,我正在使用 IntelliJ,并且已经在硬盘驱动器上启用了 cURL(允许我从任何目录访问它);提示我尝试以下操作:

C:\Users\me\scala\play\my-project>curl -v http://localhost:9000/

哪个returns:

  • timeout on name lookup is not supported
  • Trying ::1...
  • TCP_NODELAY set
  • connect to ::1 port 9000 failed: Connection refused
  • Trying 127.0.0.1...
  • TCP_NODELAY set
  • connect to 127.0.0.1 port 9000 failed: Connection refused
  • Failed to connect to localhost port 9000: Connection refused
  • Closing connection 0 curl: (7) Failed to connect to localhost port 9000: Connection refused

看来9000端口没有开放。我现在认为这需要在 intelliJ 中进行配置 - here - 但这看起来好像只在终极版上受支持。这可能是我遇到的问题的原因。这可能对其他人有帮助,但如果有人确实有更多信息,最好确定这一点。

尝试以下服务器代码,同时 运行 curl:

import java.net._
import java.io._
import scala.io._

val server = new ServerSocket(9000)
while (true) {
    val serv = server.accept()

    val in = new BufferedSource(serv.getInputStream()).getLines()
    val out = new PrintStream(serv.getOutputStream())

    out.println(in.next())
    out.flush()
    serv.close()
}

这意味着您的应用程序尚未启动(因此它实际上并未在端口 9000 上侦听)

您应该在 IntelliJ 中添加一个 运行 配置 (Run > Edit Configurations > + > Play 2 App) 并且 运行 它之前进行 curl 调用。

启动应用程序的另一种方法是在项目文件夹中打开一个终端,然后 运行 sbt run.