首页 > 代码库 > get,post提交请求
get,post提交请求
代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Text;using System.Net;using System.IO;using System.Security.Cryptography;namespace Test{ public partial class WebForm3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //string url = "http://111.206.37.135:8080/business/api/ordercreate"; //string paras = "createTime%3d2014-12-16+11%3a07%3a52%26orderList%3d%5b%7bagentName%3a%e6%90%ba%e7%a8%8b%2citemAmount%3a1%2citemId%3a265802%2citemImgUrl%3a%2citemName%3a%e6%b2%99%e7%89%b9%e9%a6%86%e2%80%9c%e6%9c%88%e4%ba%ae%e8%88%b9%e2%80%9d%e5%95%86%e5%8a%a1VIP%e4%b8%93%e4%ba%ab%e6%9c%8d%e5%8a%a1%2citemOrderTime%3a2014-12-25%2citemPrice%3a12000.00%2citemUrl%3ahttp%3a%2f%2fpiao.ctrip.com%2fdest%2ft138822.html%2citemUrlWap%3ahttp%3a%2f%2fm.ctrip.com%2fwebapp%2fticket%2fdest%2ft138822.html%7d%5d%26Ordername%3d%e4%b8%8a%e6%b5%b7%e4%b8%96%e5%8d%9a%e4%bc%9a%e6%b2%99%e7%89%b9%e9%a6%86%e2%80%9c%e6%9c%88%e4%ba%ae%e8%88%b9%e2%80%9dVIP%e6%9c%8d%e5%8a%a1%26partnerId%3dCtrip%26payWay%3d%e5%9c%a8%e7%ba%bf%e6%94%af%e4%bb%98%26srcOrderId%3d1143395377%26status%3d104%26totalAmount%3d12000.00%26sign%3db9821727999bce3ab9748a2c332cbab"; //string result = SendByPost(url, paras); string url = "http://www.baidu.com"; string result = SendByGet(url); } /// <summary> /// POST方式提交请求 /// </summary> /// <param name="url"></param> /// <param name="result"></param> /// <returns></returns> public string SendByPost(string url,string paras) { string result = string.Empty; try { System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(url); //Post请求方式 request.Method = "POST"; // 内容类型 request.ContentType = "application/x-www-form-urlencoded"; // 参数经过URL编码 //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword"); //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode("多月"); byte[] payload; //将URL编码后的字符串转化为字节 payload = System.Text.Encoding.UTF8.GetBytes(paras); //设置请求的 ContentLength request.ContentLength = payload.Length; //获得请 求流 System.IO.Stream writer = request.GetRequestStream(); //将请求参数写入流 writer.Write(payload, 0, payload.Length); // 关闭请求流 writer.Close(); System.Net.HttpWebResponse response; // 获得响应流 response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); result = myreader.ReadToEnd(); myreader.Close(); } catch (Exception ex) { result = ex.ToString(); } return result; } /// <summary> /// GET方式提交请求 /// </summary> /// <param name="url">注意url必须含http或https</param> /// <returns></returns> public string SendByGet(string url) { string result = string.Empty; try { //System.Net.HttpWebRequest request; //// 创建一个HTTP请求 //request = (System.Net.HttpWebRequest)WebRequest.Create(url); ////request.Method="get"; //System.Net.HttpWebResponse response; //response = (System.Net.HttpWebResponse)request.GetResponse(); //System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); //result = myreader.ReadToEnd(); //myreader.Close(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; ////设置代理 //if (CommonFunc.ConvertObjectToString(ConfigurationManager.AppSettings["IsProxy"]).ToUpper() == "T") //{ // string ProxyAddress = ConfigurationManager.AppSettings["ProxyAddress"].ToString().Trim(); // if (!string.IsNullOrEmpty(ProxyAddress)) // { // req.UseDefaultCredentials = true; // WebProxy proxy = new WebProxy(ProxyAddress, 8080); // req.Proxy = proxy; // } // logger.Debug("代理", "设置代理服务"); //} req.KeepAlive = true; req.Timeout = 300000; req.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); Encoding encoding = null; try { encoding = Encoding.GetEncoding(rsp.CharacterSet); } catch { encoding = Encoding.UTF8; } Stream stream = rsp.GetResponseStream(); StreamReader reader = new StreamReader(stream, encoding); result = reader.ReadToEnd(); reader.Close(); stream.Close(); rsp.Close(); } catch (Exception ex) { result = ex.Message; } return result; } }}
get,post提交请求
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。