///////post页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Net;
public partial class urlpost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string test = RequestPost("5", "http://localhost:1303/txweb/getURL.aspx", "");
}
}
#region 向Url发送post请求,返回网站响应内容
/// <summary>
/// 向Url发送post请求,返回网站响应内容
/// </summary>
/// <param name="postData">发送数据</param>
/// <param name="uriStr">接受数据的Url</param>
/// <param name="action">更新操作</param>
/// <returns>返回网站响应内容</returns>
public static string RequestPost(string postData, string uriStr, string action)
{
HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create(uriStr);
StringBuilder postContent = new StringBuilder();
Encoding myEncoding = Encoding.GetEncoding("gb2312");
postContent.Append(HttpUtility.UrlEncode("message", myEncoding));
postContent.Append("=");
postContent.Append(HttpUtility.UrlEncode(postData, myEncoding));
//postContent.Append("&");
//postContent.Append(HttpUtility.UrlEncode("type", myEncoding));
//postContent.Append("=");
//postContent.Append(HttpUtility.UrlEncode("sync", myEncoding));
//postContent.Append("&");
//postContent.Append(HttpUtility.UrlEncode("action", myEncoding));
//postContent.Append("=");
//postContent.Append(HttpUtility.UrlEncode(action, myEncoding));
requestScore.Headers.Add("qb_count", QB); //header传参 重要
byte[] data = Encoding.ASCII.GetBytes(postContent.ToString());
requestScore.Method = "Post";
requestScore.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
requestScore.ContentLength = data.Length;
requestScore.KeepAlive = true;
Stream stream = requestScore.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse responseSorce;
try
{
responseSorce = (HttpWebResponse)requestScore.GetResponse();
}
catch (WebException ex)
{
responseSorce = (HttpWebResponse)ex.Response;//得到请求网站的详细错误提示
}
StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
requestScore.Abort();
responseSorce.Close();
responseSorce.Close();
reader.Dispose();
stream.Dispose();
return content;
}
#endregion
}
/////get页面 网站接收数据:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class getURL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string message = Request.Form["message"];
}
}
}
////////不带参数的body接收
if (!IsPostBack)
{
System.IO.Stream sm = Request.InputStream;//获取post正文
int len = (int)sm.Length;//post数据长度
byte[] inputByts = new byte[len];//字节数据,用于存储post数据
sm.Read(inputByts, 0, len);//将post数据写入byte数组中
sm.Close();//关闭IO流
//**********下面是把字节数组类型转换成字符串**********
string data = System.Text.Encoding.GetEncoding("utf-8").GetString(inputByts);//转为unicode编码
data = Server.UrlDecode(data);//下面解释一下Server.UrlDecode和Server.UrlEncode的作用
////////数据解密
string srt = Des3Encode.Des3.strOutCODE(data);
}
/// <summary>
/// 以GET 形式获取数据
/// </summary>
/// <param name="RequestPara"></param>
/// <param name="Url"></param>
/// <returns></returns>
public static string GetData(string RequestPara, string Url)
{
RequestPara=RequestPara.IndexOf('?')>-1?(RequestPara):("?"+RequestPara);
WebRequest hr = HttpWebRequest.Create(Url + RequestPara);
byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);
hr.Method = "GET";
System.Net.WebResponse response = hr.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
string ReturnVal = reader.ReadToEnd();
reader.Close();
response.Close();
return ReturnVal;
}