后端开发
网络新概念,云计算、大数据、O2O、电商。。。。
网络新概念,云计算、大数据、O2O、电商。。。。
2020-11-02 15:13:42
String urlreq = "" WebHeaderCollection header = new WebHeaderCollection(); header["atoken"] = TOKEN; JObject Jsonresult = GETUrl(urlreq,header); Console.WriteLine(Jsonresult.ToString()); String jsonStr = "{\"appkey\":\"x221211212\",\"username\":\"xx\",\"password\":\"xx\"}"; String urlreq = ""; Console.WriteLine(jsonStr); JObject Jsonresult = PostUrl(urlreq, jsonStr); Console.WriteLine(Jsonresult.ToString()); /* * URL:POST请求地址 * postData:json格式的请求报文,例如:{"key1":"value1","key2":"value2"} */ public static JObject PostUrl(string url, string postData) { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; //req.Timeout = 80;//设置请求超时时间,单位为毫秒 req.ContentType = "application/json"; byte[] data = Encoding.UTF8.GetBytes(postData); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return JObject.Parse(result); } /* * URL:GET请求地址 */ public static JObject GETUrl(string url,WebHeaderCollection heades) { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Headers = heades; req.Method = "GET"; //req.Timeout = 80;//设置请求超时时间,单位为毫秒 req.ContentType = "application/json"; HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return JObject.Parse(result); }