简体   繁体   中英

how i can retrive the passed data from UploadStringTaskAsync call

I have the following 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. so on the receiver Post action method i am unable to retrieve the FQDN parameter,

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)

If FQDN is a query parameter then try

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.

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 ; something like

var s = Encoding.Utf8.GetString(json.Result);

For a simple query param, you can do as @Richard has said

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

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

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> . 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM