[英]how i can retrive the passed data from UploadStringTaskAsync call
I have the following webclient :- 我有以下的webclient:
using (WebClient wc = new WebClient())
{
var url = currentURL+ "home/scanserver";
wc.Headers.Add("Authorization", token);
var json =wc.UploadStringTaskAsync(url, "FQDN=allscan");
}
now i am passing a security token & a data named FQDN. 现在,我正在传递安全令牌和名为FQDN的数据。 so on the receiver Post action method i am unable to retrieve the FQDN parameter,
所以在接收者的Post action方法上,我无法检索FQDN参数,
i tried the following but did not work 我尝试了以下但没有用
string FQDN = Request.Form["FQDN"];
also I tried defining the FQDN inside the action method parameter as follow:- public async Task<ActionResult> ScanServer(string FQDN)
我也尝试在动作方法参数内定义FQDN,如下所示:-
public async Task<ActionResult> ScanServer(string FQDN)
If FQDN is a query parameter
then try 如果FQDN是
query parameter
尝试
using (WebClient wc = new WebClient())
{
var url = currentURL+ "home/scanserver?FQDN=allscan";
wc.Headers.Add("Authorization", token);
var json =wc.UploadStringTaskAsync(url, "");
}
EDIT 编辑
To post as a form use the UploadValuesTaskAsync method. 要将其发布为表单,请使用UploadValuesTaskAsync方法。
var url = currentURL+ "home/scanserver";
var args = new NameValueCollection { { "FQDN", "allscan" } };
var json = wc.UploadValuesTaskAsync(url, args);
json
is now a Task<byte>
not a Task<string>
, so you must convert it into a string
; json
现在是一个Task<byte>
而不是Task<string>
,因此您必须将其转换为string
; something like 就像是
var s = Encoding.Utf8.GetString(json.Result);
For a simple query param, you can do as @Richard has said 对于简单的查询参数,您可以按照@Richard所说的进行
using (WebClient wc = new WebClient())
{
var url = currentURL+ "home/scanserver?FQDN=allscan";
wc.Headers.Add("Authorization", token);
var json =wc.UploadStringTaskAsync(url, "");
}
Or, if you want to send a long string, or a complex object MyObject
then you can do like 或者,如果您要发送长字符串或复杂的对象
MyObject
则可以执行以下操作
using (WebClient wc = new WebClient())
{
var url = currentURL+ "home/scanserver";
wc.Headers.Add("Authorization", token);
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
//here you can also pass a json serialized complex object
var json =wc.UploadStringTaskAsync(url, "{FQDN:'allscan'}");
}
I'm assuming here, that your api method looks like 我假设在这里,您的api方法看起来像
public void scanserver(string FQDN) //OR (MyClass myObject)
{
//do something with token and FQDN
//OR use your complex object - myObject
}
The return value from UploadStringTaskAsync
is Task<string>
. UploadStringTaskAsync
的返回值为Task<string>
。 You will need to either await the returned task, add a continuation, or block the thread to wait for the result to be returned. 您将需要等待返回的任务,添加延续或阻塞线程以等待返回结果。
Blocking method: 封锁方式:
using (WebClient wc = new WebClient())
{
var url = currentURL+ "home/scanserver";
wc.Headers.Add("Authorization", token);
var json = wc.UploadStringTaskAsync(url, "FQDN=allscan").Result;
}
Using await (must be inside a method marked as async): 使用等待(必须在标记为异步的方法内):
string json;
using (WebClient wc = new WebClient())
{
var url = currentURL + "home/scanserver";
wc.Headers.Add("Authorization", token);
json = await wc.UploadStringTaskAsync(url, "FQDN=allscan");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.