12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- namespace EMISOnline.WinService.Common
- {
- public class HttpHelper
- {
- public static string GetUrltoText(string Url, string encode)
- {
- try
- {
- System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
- // Get the response instance.
- System.Net.WebResponse wResp = wReq.GetResponse();
- System.IO.Stream respStream = wResp.GetResponseStream();
- // Dim reader As StreamReader = New StreamReader(respStream)
- using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(encode)))
- {
- return reader.ReadToEnd();
- }
- }
- catch (System.Exception ex)
- {
- //errorMsg = ex.Message;
- }
- return "";
- }
- public static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
- {
- string ret = string.Empty;
- try
- {
- byte[] byteArray = dataEncode.GetBytes(paramData); //转化
- HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
- webReq.Method = "POST";
- webReq.ContentType = "application/x-www-form-urlencoded";
- webReq.ContentLength = byteArray.Length;
- Stream newStream = webReq.GetRequestStream();
- newStream.Write(byteArray, 0, byteArray.Length);//写入参数
- newStream.Close();
- HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
- StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
- ret = sr.ReadToEnd();
- sr.Close();
- response.Close();
- newStream.Close();
- }
- catch (Exception ex)
- {
- }
- return ret;
- }
- }
- }
|