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