简体   繁体   English

我想在我的Windows窗体中包含HttpContext.Current.Request

[英]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. 我想反序列化json值。所以我使用上面的代码。当我构建'请求在当前上下文中不存在'错误时显示。

In the code in your question, you're successfully deserializing the JSON string you receive from a web request you issue through HttpWebRequest. 在您的问题的代码中,您已成功反序列化从通过HttpWebRequest发出的Web请求中收到的JSON字符串。

Say the response contains { "foo" : "bar" } , then that's the value the variable json contains. 假设响应包含{ "foo" : "bar" } ,那么这是变量json包含的值。

But the expression Request[json] that follows it, makes no sense. 但是跟随它的表达式Request[json]没有任何意义。 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. 我可以向您保证,请求变量不包含名为{ "foo" : "bar" } ,因此表达式Request[json]返回一个空字符串。

You should not use Request there, but directly pass the json variable: 你不应该在那里使用Request ,而是直接传递json变量:

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. 所以你不需要HttpRequest.Current,因为json字符串与当前请求完全没有关系。

firstly you can not use HttpContext.Current in win form. 首先你不能在win形式中使用HttpContext.Current。 winform not running in request pipeline.. winform没有在请求管道中运行..

DeserializeObject waiting serialize string and type.. DeserializeObject等待序列化字符串并输入..

    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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用动态重定向HttpContext.Current.Request吗? - can I use dynamic to redirect HttpContext.Current.Request? 生产中的HttpContext.Current.Request为null - HttpContext.Current.Request null in production 在HttpContext.Current.Request中模拟ServerVariables - Mock ServerVariables in the HttpContext.Current.Request HttpContext.Request或HttpContext.Current.Request URL中是否缺少“#”字母? - '#' letter is missing in HttpContext.Request or HttpContext.Current.Request url? HttpControllerContext.Request和HttpContext.Current.Request之间的区别 - Difference between HttpControllerContext.Request and HttpContext.Current.Request HttpContext.Current.Request和Page.Request中的Url.Host - Url.Host in HttpContext.Current.Request and Page.Request 如何获取超过1000个html控件值的HttpContext.Current.Request? - how can i get HttpContext.Current.Request of more than 1000 html controls value? 带有Angular UI路由器的HttpContext.Current.Request - HttpContext.Current.Request with Angular ui-router 什么HttpContext.Current.Request [“ CarName”]!= null,它在做什么? - what HttpContext.Current.Request[“CarName”] != null, what it is doing? HttpContext.Current.Items []和HttpContext.Current.Request []之间有什么区别? - What's the differences between HttpContext.Current.Items[] and HttpContext.Current.Request[]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM