c# - 无法从 'string' 转换为 'System.Net.Http.HttpRequestMessage'

标签 c# asp.net .net xamarin xamarin.android

我真的是 c# 的新手,并且通常会发出 API 请求,所以不确定我的代码是否有意义。现在我只是想获取我从 API 的 URL 调用中获得的数据并将其显示到 TextView 上称为 testText .我愿意使用 RestSharpHttpClient ,无论我能做什么。

public class MainActivity : Activity
{

    HttpClient client;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
    }

    public async Task<string> GetResponseString()
    {
        var request = "http://localhost:51843/api/values/getMessage?id=1";
        var response = await client.SendAsync(request);
        var content = await response.Content.ReadAsStringAsync();
        return content;
    }
}

在我的公众内部async Task<>方法是我得到

Cannot convert from 'string' to 'System.Net.Http.HttpRequestMessage'

request 错误多变的。我在所有其他尝试的评论中留下了评论,让我知道我是否应该尝试其中的任何一个

最佳答案

SendAsync 有一个参数 HttpRequestMessage,而您正在传递一个字符串。这正是错误告诉您的内容。

如果你查找 HttpRequestMessage , 它需要一个方法和一个 URI

var url = "http://localhost:51843/api/values/getMessage?id=1";
var req = new HttpRequestMessage(HttpMethod.POST, new Uri(url));

关于c# - 无法从 'string' 转换为 'System.Net.Http.HttpRequestMessage',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45287174/

相关文章:

c# - 从客户端应用程序自动安全地登录 Web

c# - 事件处理程序始终为空

asp.net - 如何设置 FormattedText 的行高

c# - System.Numerics 是一个单独的程序集的原因

c# - 在 C# 中编写递归函数

c# - 在 C# 中读取随机访问文件

c# - 如何在 linq 中将空列表视为空列表?

c# - 如何将按钮命令绑定(bind)到成员方法?

C# HttpClient 损坏的管道,但 curl 有效

c# - 报告 Directory.GetFiles 的进度