使用的post传url+string的
public static string GetPage(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;
// Response.Write(content);
return content;
}
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}
在腾讯文档里面会涉及到的sha256编码,使用代码如下:
string test=”。。。。。。“;
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(test); //sha256加密
SHA256 sha256 = new SHA256CryptoServiceProvider();
byte[] retVal = sha256.ComputeHash(bytValue);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
string sigtext = sb.ToString();
把string转成utf-8类型
public static string get_uft8(string unicodeString)
{
UTF8Encoding utf8 = new UTF8Encoding();
Byte[] encodedBytes = utf8.GetBytes(unicodeString);
String decodedString = utf8.GetString(encodedBytes);
return decodedString;
}
把当前日期转成时间戳
public static string ConvertDateTimeToUnix(DateTime time)//转时间戳
{
return ((time.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
}
使用服务器端传来的数据使用,后面直接调用rs.**就可以了
byte[] byts = new byte[Request.InputStream.Length];
Request.InputStream.Read(byts, 0, byts.Length);
string req = System.Text.Encoding.Default.GetString(byts);
req = Server.UrlDecode(req);
JavaScriptSerializer serializer = new JavaScriptSerializer();
cReq rs = serializer.Deserialize(req);
解析Json数据,要使用using Newtonsoft.Json;
Newtonsoft.Json.Linq.JObject jo = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(result);
get传url
public static string GetHtmlSource(string url)
{
//处理内容
string html = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*"; //接受任意文件
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; // 模拟使用IE在浏览 http://www.52mvc.com
request.AllowAutoRedirect = true;//是否允许302
//request.CookieContainer = new CookieContainer();//cookie容器,
request.Referer = url; //当前页面的引用
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.Default);
html = reader.ReadToEnd();
stream.Close();
return html;
}
post传键值对
public static string PostHtml(string url,NameValueCollection data)
{
string responseInString = "";
using (var wb = new WebClient())
{
//var data = new NameValueCollection();
//data["appid"] = "To2rrUGuNC99Fj90";
//data["appsecret"] = "aVZQ36CrXwTXziCY";
//data["grant_type"] = "authorization_code";
//data["code"] = "bb95715d6a90226e67bc8d39ed4f7ab1";
var response = wb.UploadValues(url, "POST", data);
responseInString = Encoding.UTF8.GetString(response);
}
return responseInString;
}
网友评论