C#客户端GO服务器
我是新来的,并试图学习它。我编写了简单的Hello world服务器,并试图通过我的C#客户端GO服务器,c#,C#,我是新来的,并试图学习它。我编写了简单的Hello world服务器,并试图通过我的C#控制台应用程序访问它。这是我的服务器: package main import ( "fmt" "net/" "github.com/labstack/echo" ) func main(){ fmt.Println("Welcome to the Server!") e:=echo.New() e.GET("/", func(c echo.Cont
C#控制台应用程序
访问它。这是我的服务器:
package main
import (
"fmt"
"net/"
"github.com/labstack/echo"
)
func main(){
fmt.Println("Welcome to the Server!")
e:=echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Yallo from the Server! \n")
})
}
我正在为此使用labstack/echo
软件包。它与我的Mozilla
配合使用。
这是我的客户
static void Main(string[] args)
{
var client = new HttpClient();
string responseString = string.Empty;
var task = new Task(async () =>
{
responseString = await client.GetStringAsync("localhost:8000");
});
task.Start();
task.Wait();
Console.WriteLine(responseString);
Console.ReadKey();
}
但我得到了一个错误:
System.Net.Http.dll中发生“System.ArgumentException”类型的异常,但未在用户代码中处理
只允许使用“http”和“https”方案
注意,该方法需要一个URI,并且需要一个协议/方案。换句话说,请尝试以下方法:
responseString=wait client.GetStringAsync(“http://localhost:8000");
async
关键字导致控制台应用程序出现问题。我不知道为什么,每个人都说了不同的话。这会奏效的
var client = new HttpClient();
string responseString = string.Empty;
responseString = client.GetStringAsync("http://localhost:8000").Result;
Console.WriteLine(responseString);
Console.ReadKey();
更改
client.GetStringAsync(“localhost:8000”)代码>到client.GetStringAsync(“http://localhost:8000");代码>Yeap这不会给出错误,但只给出一个空字符串。我建议您将此问题分解,以便隔离问题。例如,从同步调用开始(没有任务和等待,您仍然在为它阻塞),然后查看您的服务器是否返回了预期值。然后,您可以担心使客户机变得更复杂。:)如上所述,服务器正在返回Mozilla中的字符串。你是说GetStringAsync
方法已经在等待它的完成了吗?啊,别介意我的建议:HttpClient的检索方法都是异步的。好的。我的C#client中的空字符串呢。有什么建议吗?