我尝试使用添加的参数执行GET。我之前使用的WebClient代码突然停止工作,所以我决定转而使用HttpWebRequest / HttpWebResponse。你如何正确添加参数?我有一个REST函数,它接受两个字符串参数,但我无法看到它们。基本上,如何向GET调用添加多个参数?
这是我的GET代码(booksString是以逗号分隔的书籍ID字符串):
string webAddress = "http://localhost:5000/stuff/address";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webAddress);
request.Headers.Add("bookIds", booksString);
request.Method = "GET";
request.Accept = "text/html";
request.KeepAlive = true;
request.ProtocolVersion = HttpVersion.Version10;
string text = "";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(),
Encoding.ASCII))
{
text = reader.ReadToEnd();
}
}
Console.WriteLine("text: " + text);
这是我的REST代码:
public List<Book> GetBooks(string bookIds, string param2)
{
Console.WriteLine("book IDs: " + bookIds + " " + param2);
}
每当我运行此代码时bookIds
为空?如何发送多个参数并让我的REST功能识别数据?
答案 0 :(得分:1)
尝试使用RestSharp Nuget Package,它非常巧妙地封装了HTTPWeb请求。
你可以这样做
var baseUrl = "http://localhost:5000/";
var resource = "stuff/address";
var api = new RestClient(baseUrl);
var request = new RestRequest(resource, Method.GET);
request.AddQueryParameter("bookIds", bookString);
request.AddQueryParameter("param2", value);
api.Execute(request);
答案 1 :(得分:1)
尝试使用网址编码。
http://localhost:5000/stuff/address?bookIds= {0}&安培; param2的= {1}
使用string.format填充值并进行调用。