简体   繁体   中英

I want to include HttpContext.Current.Request in my windows forms

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{    
    string json= streamReader.ReadToEnd();
    List<DeSerialiseBL> myDeserializedObjList = (List<DeSerialiseBL>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request[json], typeof(List<DeSerialiseBL>));
}

i want to deserialize the json value.so i use the above code.when i build 'Request is not exist in the current context' error displayed.

In the code in your question, you're successfully deserializing the JSON string you receive from a web request you issue through HttpWebRequest.

Say the response contains { "foo" : "bar" } , then that's the value the variable json contains.

But the expression Request[json] that follows it, makes no sense. I can assure you that the request variables do not contain a key called { "foo" : "bar" } , so the expression Request[json] returns an empty string.

You should not use Request there, but directly pass the json variable:

List<DeSerialiseBL> myDeserializedObjList = 
    (List<DeSerialiseBL>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<DeSerialiseBL>));

The code can be further simplified:

using Newtonsoft.Json; // At the top of your file

var myDeserializedObjList = JsonConvert.DeserializeObject<List<DeSerialiseBL>>(json);

So you don't need HttpRequest.Current, because the json string has no relation at all to the current request.

firstly you can not use HttpContext.Current in win form. winform not running in request pipeline..

DeserializeObject waiting serialize string and type..

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {    
         string json= streamReader.ReadToEnd();
         //List<DeSerialiseBL> myDeserializedObjList = (List<DeSerialiseBL>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request[json], typeof(List<DeSerialiseBL>));
         List<DeSerialiseBL> myDeserializedObjList = (List<DeSerialiseBL>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<DeSerialiseBL>));
    }

try this

 string Url = "Your Url";

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(Url);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = client.GetAsync(Url).Result;
    if (response.IsSuccessStatusCode)
    {
        var JsonResult = response.Content.ReadAsStringAsync().Result;
        System.Web.Script.Serialization.JavaScriptSerializer tmp = new System.Web.Script.Serialization.JavaScriptSerializer();
        RemoteResult r = (RemoteResult)tmp.Deserialize(JsonResult, typeof(RemoteResult));
         // r.myDeserializedObjList is your desired output
    }

and create class for your desired result.That is

public class RemoteResult
    {
        List<DeSerialiseBL> myDeserializedObjList;
    }

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