| | |
| | | |
| | | #endregion |
| | | |
| | | public string WebPost(string url, string postData, string cotentType = "application/json") { |
| | | #region WebHelper |
| | | public string WebHttpPost(string url, string postData, string cotentType = "application/json") |
| | | { |
| | | Console.WriteLine(url); |
| | | WebRequest request = WebRequest.Create(url); |
| | | request.Method = "POST"; |
| | |
| | | request.ContentLength = byteArray.Length; |
| | | request.Timeout = 3000; |
| | | |
| | | try { |
| | | try |
| | | { |
| | | Stream dataStream = request.GetRequestStream(); |
| | | dataStream.Write(byteArray, 0, byteArray.Length); |
| | | dataStream.Close(); |
| | |
| | | response.Close(); |
| | | return responseFromServer; |
| | | } |
| | | catch (Exception e) { |
| | | catch (Exception e) |
| | | { |
| | | Console.WriteLine(e.Message); |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | public string WebGet(string url) { |
| | | public string WebGet(string url) |
| | | { |
| | | //using (var client = new HttpClient()) { |
| | | // //请求结果 |
| | | // string result = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result; |
| | |
| | | request.Timeout = 6000; |
| | | request.Method = "GET"; |
| | | |
| | | try { |
| | | try |
| | | { |
| | | WebResponse response = request.GetResponse(); |
| | | Stream dataStream = response.GetResponseStream(); |
| | | StreamReader reader = new StreamReader(dataStream); |
| | |
| | | Console.WriteLine(responseFromServer); |
| | | return responseFromServer; |
| | | } |
| | | catch (Exception e) { |
| | | catch (Exception e) |
| | | { |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | public string Get(string url, string contentType = "application/x-www-form-urlencoded") { |
| | | public string Get(string url, string contentType = "application/x-www-form-urlencoded") |
| | | { |
| | | WebRequest request = WebRequest.Create(url); |
| | | request.Method = "Get"; |
| | | request.ContentType = contentType; |
| | | StreamReader reader = null; |
| | | Stream stream = null; |
| | | WebResponse rsp = null; |
| | | try { |
| | | try |
| | | { |
| | | |
| | | rsp = request.GetResponse(); |
| | | stream = rsp.GetResponseStream(); |
| | | reader = new StreamReader(stream); |
| | | return reader.ReadToEnd(); |
| | | } |
| | | catch { |
| | | catch |
| | | { |
| | | return ""; |
| | | } |
| | | finally { |
| | | finally |
| | | { |
| | | // 释放资源 |
| | | if (reader != null) reader.Close(); |
| | | if (stream != null) stream.Close(); |
| | |
| | | |
| | | } |
| | | |
| | | public string Post(string url, string postData, string contentType = "application/json", string sessionId = "") { |
| | | public string Post(string url, string postData, string contentType = "application/json", string sessionId = "") |
| | | { |
| | | Console.WriteLine(url); |
| | | WebRequest request = WebRequest.Create(url); |
| | | request.Method = "POST"; |
| | |
| | | request.ContentType = contentType; |
| | | request.ContentLength = byteArray.Length; |
| | | request.Timeout = 3000; |
| | | if (sessionId != "") { |
| | | if (sessionId != "") |
| | | { |
| | | request.Headers.Set("ASP.NET_SessionId", sessionId); |
| | | } |
| | | StreamReader reader = null; |
| | | Stream stream = null; |
| | | WebResponse rsp = null; |
| | | try { |
| | | try |
| | | { |
| | | stream = request.GetRequestStream(); |
| | | stream.Write(byteArray, 0, byteArray.Length); |
| | | stream.Close(); |
| | |
| | | reader = new StreamReader(stream); |
| | | return reader.ReadToEnd(); |
| | | } |
| | | catch (Exception ex) { |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"{url} err={ex.Message}"); |
| | | return ""; |
| | | } |
| | | finally { |
| | | finally |
| | | { |
| | | // 释放资源 |
| | | if (reader != null) reader.Close(); |
| | | if (stream != null) stream.Close(); |
| | |
| | | } |
| | | |
| | | |
| | | public string Post(string url, Dictionary<string, string> dic) { |
| | | public string Post(string url, Dictionary<string, string> dic) |
| | | { |
| | | var param = dic.Select(a => { return string.Format("{0}={1}", a.Key, a.Value); }).ToList(); |
| | | return Post(url, string.Join("&", param), "application/x-www-form-urlencoded"); |
| | | |
| | | } |
| | | |
| | | public string PostWithCookie(string url, string sessinId) { |
| | | public string PostWithCookie(string url, string sessinId) |
| | | { |
| | | return PostWithCookie(url, "", "", sessinId); |
| | | } |
| | | public string PostWithCookie(string url, string postData, string contentType = "application/json", string sessionId = "") { |
| | | public string PostWithCookie(string url, string postData, string contentType = "application/json", string sessionId = "") |
| | | { |
| | | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); |
| | | request.Method = "POST"; |
| | | byte[] byteArray = Encoding.UTF8.GetBytes(postData); |
| | | request.ContentType = contentType; |
| | | request.ContentLength = byteArray.Length; |
| | | if (sessionId != "") { |
| | | if (sessionId != "") |
| | | { |
| | | request.CookieContainer = new CookieContainer(); |
| | | request.CookieContainer.SetCookies(new Uri("http://" + request.RequestUri.Authority), "ASP.NET_SessionId=" + sessionId); |
| | | } |
| | | StreamReader reader = null; |
| | | Stream stream = null; |
| | | WebResponse rsp = null; |
| | | try { |
| | | try |
| | | { |
| | | stream = request.GetRequestStream(); |
| | | stream.Write(byteArray, 0, byteArray.Length); |
| | | stream.Close(); |
| | |
| | | reader = new StreamReader(stream); |
| | | return reader.ReadToEnd(); |
| | | } |
| | | catch { |
| | | catch |
| | | { |
| | | return ""; |
| | | } |
| | | finally { |
| | | finally |
| | | { |
| | | // 释放资源 |
| | | if (reader != null) reader.Close(); |
| | | if (stream != null) stream.Close(); |
| | |
| | | } |
| | | |
| | | } |
| | | #endregion |
| | | |
| | | |
| | | |
| | | } |