Http 请求

 public static string HttpGet(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";//设置请求的方法
request.Accept = "*/*";//设置Accept标头的值
string responseStr = "";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())//获取响应
{
using (StreamReader reader =
new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
responseStr = reader.ReadToEnd();
}
}
return responseStr;
}
  public static string HttpPost(string url, string  data)
{
//当请求为https时,验证服务器证书
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((a, b, c, d) => true);
var req= (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "*/*";
req.Timeout = ;
req.AllowAutoRedirect = false;
string responseStr = "";
using (var writer = new StreamWriter(req.GetRequestStream()))
{
writer.Write(data);
}
using (var res = (HttpWebResponse)req.GetResponse())
{
using (var reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
{
responseStr = reader.ReadToEnd();//获取响应
}
}
return responseStr;
}
   public static string HttpPost(string url, string stream)
{
//当请求为https时,验证服务器证书
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((a, b, c, d) => true);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.Timeout = ;
request.AllowAutoRedirect = false;
string responseStr = "";
using (var reqstream = request.GetRequestStream())
{
stream.Position = 0L;
stream.CopyTo(reqstream);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
responseStr = reader.ReadToEnd();//获取响应
}
}
return responseStr;
}
 public static string HttpPostForm(string url, List<FormEntity> form)
{
//分割字符串
var boundary = "----" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback((a, b, c, d) => true);
request.ContentType = "multipart/form-data; boundary=" + boundary;
MemoryStream stream = new MemoryStream();
#region 将非文件表单写入内存流
foreach (var entity in form.Where(f => f.IsFile == false))
{
var temp = new StringBuilder();
temp.AppendFormat("\r\n--{0}", boundary);
temp.Append("\r\nContent-Disposition: form-data;");
temp.AppendFormat("name=\"{0}\"", entity.Name);
temp.Append("\r\n\r\n");
temp.Append(entity.Value);
byte[] b = Encoding.UTF8.GetBytes(temp.ToString());
stream.Write(b, , b.Length);
}
#endregion
#region 将文件表单写入内存流
foreach (var entity in form.Where(f => f.IsFile == true))
{
byte[] filedata = null;
var filename = Path.GetFileName(entity.Value);
//表示是网络资源
if (entity.Value.Contains("http"))
{
//处理网络文件
using (var client = new WebClient())
{
filedata = client.DownloadData(entity.Value);
}
}
else
{
//处理物理路径文件
using (FileStream file = new FileStream(entity.Value,
FileMode.Open, FileAccess.Read))
{
filedata = new byte[file.Length];
file.Read(filedata, , (int)file.Length);
}
}
var temp = string.Format("\r\n--{0}\r\nContent-Disposition: " +
"form-data; name=\"{1}\"; filename=\"{2}\"\r\n\r\n",
boundary, entity.Name, filename);
byte[] b = Encoding.UTF8.GetBytes(temp);
stream.Write(b, , b.Length);
stream.Write(filedata, , filedata.Length);
}
#endregion
//结束标记
byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary +
"--\r\n");
stream.Write(foot_data, , foot_data.Length);
Stream reqStream = request.GetRequestStream();
stream.Position = 0L;
//将Form表单生成流写入请求流
stream.CopyTo(reqStream);
stream.Close();
reqStream.Close();
using (HttpWebResponse response = (HttpWebResponse)request.
GetResponse())
{
using (StreamReader reader =
new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();//获取响应
}
}
}
上一篇:缓存:修改Hosts不生效


下一篇:C#中listbox中选中多项,并删除