码迷,mamicode.com
首页 > Windows程序 > 详细

C# 向服务器发送信息

时间:2015-03-13 16:21:16      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

#region 向服务器发送信息
/// <summary>
/// 向服务器发送信息
/// </summary>
/// <param name="posturl">请求路径</param>
/// <param name="postData">请求参数</param>
/// <returns>返回信息</returns>
public string SendInfo(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
return string.Empty;
}
}
#endregion

C# 向服务器发送信息

标签:

原文地址:http://www.cnblogs.com/a-mumu/p/4335089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!