-1

我正在尝试测试Titanium Proxy Server的示例。但是,在完全复制“自述”部分中的示例后,我遇到了一个无法解决的错误。

在此方法中:

public async Task OnRequest(object sender, SessionEventArgs e)
{
    Console.WriteLine(e.WebSession.Request.Url);
    var requestHeaders = e.WebSession.Request.Headers;
    var method = e.WebSession.Request.Method.ToUpper();
    if ((method == "POST" || method == "PUT" || method == "PATCH"))
    {
        byte[] bodyBytes = await e.GetRequestBody();
        await e.SetRequestBody(bodyBytes);
        string bodyString = await e.GetRequestBodyAsString();
        await e.SetRequestBodyString(bodyString);
        e.UserData = e.WebSession.Request;
    }
}

我收到线路错误await e.SetRequestBodyString(bodyString);await e.SetRequestBody(bodyBytes);.

有错误消息状态Cannot await 'void',它将方法中的参数SessionEventArgs称为 void 方法本身。

我该如何解决这个问题?我做错了什么,因为示例代码具体如上所写?

4

1 回答 1

0

他们的文档不正确。它是异步的(SetRequestBody,SetRequestBodyString),但现在它是同步的,你会得到这样的异常

public async Task OnRequest(object sender, SessionEventArgs e)
{
    Console.WriteLine(e.WebSession.Request.Url);
    var requestHeaders = e.WebSession.Request.Headers;
    var method = e.WebSession.Request.Method.ToUpper();
    if ((method == "POST" || method == "PUT" || method == "PATCH"))
    {
        byte[] bodyBytes = await e.GetRequestBody();
        e.SetRequestBody(bodyBytes);
        string bodyString = await e.GetRequestBodyAsString();
        e.SetRequestBodyString(bodyString);
        e.UserData = e.WebSession.Request;
    }
}
于 2018-09-28T06:35:24.097 回答