简体   繁体   English

我如何从UploadStringTaskAsync调用中检索传递的数据

[英]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.

相关问题 从Form2返回数据时,如何检索表单1的其他值? - How can I retrive other values of form 1 when returning data from form2? 无法从Azure数据库中检索数据 - Can't retrive data from Azure Database 如何从该阵列中检索信息? - How do I retrive information from this array? 如何在 C# 和 sql 中调用数据库中的数据? - How can I call the data from the database in C# and sql? Razor 页面当我从 post 方法调用它时,我丢失了从用户页面传递的用户数据 - Razor page I lost the User data Passed from the user page when I call it from the post method 如何在单击按钮时从datagridview中的sqlserver检索所有类型的数据? - How to retrive all types of data from sqlserver in datagridview on button click? 如何基于datagridview的单元格值从数据库中检索数据 - how to retrive data from the database based on a value of the cell of the datagridview 如何将int数据从数据库检索到JavaScript中的变量? - How to retrive int data from database to a variable in javascript? 如何在控制器中显示从Viewbag传递的数据以进行查看 - How do i display data passed from viewbag in controller to view 如何从HttpResponseMessage检索传递的异常(HttpStatusCode,Exception) - How can I retrieve passed exception from HttpResponseMessage(HttpStatusCode, Exception)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM