.net 模拟GET或者POST请求 传参URL和获取返回值
来源:互联网 发布:上海交通事故数据统计 编辑:程序博客网 时间:2024/06/05 06:00
简单GET请求:
string url = "http://i******com.cn/fcps-uat/rest/eip/eipNotice?inputJson="; System.Net.WebRequest wrq = System.Net.WebRequest.Create(url + strparam); wrq.Method = "GET"; System.Net.WebResponse wrp = wrq.GetResponse(); System.IO.StreamReader sr = new System.IO.StreamReader(wrp.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8")); strResult = sr.ReadToEnd();#region 通讯函数 /// <summary>/// 通讯函数/// </summary>/// <param name="url">请求Url</param>/// <param name="para">请求参数</param>/// <param name="method">请求方式GET/POST</param>/// <returns></returns>public static string SendRequest(string url, string para, string method){string strResult = "";if (url == null || url == "")return null;if (method == null || method == "")method = "GET";// GET方式if (method.ToUpper() == "GET"){try{System.Net.WebRequest wrq = System.Net.WebRequest.Create(url + para);wrq.Method = "GET";ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;System.Net.WebResponse wrp = wrq.GetResponse();System.IO.StreamReader sr = new System.IO.StreamReader(wrp.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));strResult = sr.ReadToEnd();}catch (Exception ex){return ex.Message;}}// POST方式if (method.ToUpper() == "POST"){if (para.Length > 0 && para.IndexOf('?') == 0){para = para.Substring(1);}WebRequest req = WebRequest.Create(url);req.Method = "POST";req.ContentType = "application/x-www-form-urlencoded";ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;StringBuilder UrlEncoded = new StringBuilder();Char[] reserved = { '?', '=', '&' };byte[] SomeBytes = null;if (para != null){int i = 0, j;while (i < para.Length){j = para.IndexOfAny(reserved, i);if (j == -1){UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, para.Length - i), System.Text.Encoding.GetEncoding("gb2312")));break;}UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, j - i), System.Text.Encoding.GetEncoding("gb2312")));UrlEncoded.Append(para.Substring(j, 1));i = j + 1;}SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());req.ContentLength = SomeBytes.Length;Stream newStream = req.GetRequestStream();newStream.Write(SomeBytes, 0, SomeBytes.Length);newStream.Close();}else{req.ContentLength = 0;}try{WebResponse result = req.GetResponse();Stream ReceiveStream = result.GetResponseStream();Byte[] read = new Byte[512];int bytes = ReceiveStream.Read(read, 0, 512);while (bytes > 0){// 注意:// 下面假定响应使用 UTF-8 作为编码方式。// 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:// Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");Encoding encode = System.Text.Encoding.GetEncoding("gb2312");strResult += encode.GetString(read, 0, bytes);bytes = ReceiveStream.Read(read, 0, 512);}return strResult;}catch (Exception ex){return ex.Message;}}return strResult;}#endregion#region 简化通讯函数/// <summary>/// GET方式通讯/// </summary>/// <param name="url"></param>/// <param name="para"></param>/// <returns></returns>public static string SendRequest(string url, string para){return SendRequest(url, para, "GET");}#endregion
0 0
- .net 模拟GET或者POST请求 传参URL和获取返回值
- 通过get或post请求URL获取返回数据
- VB.NET通过URL POST请求和GET请求
- VB.NET通过URL POST请求和GET请求
- asp.net获取传值get和post请求
- asp.net获取传值get和post请求
- asp.net获取传值get和post请求
- .net获取传值get和post请求
- 模拟get和post请求,获取分页数据
- http get或者post请求获取参数
- asp.net自定义获取Get和Post请求的参数
- asp.net自定义获取Get和Post请求的参数
- .net get post 传递参数 获取返回值
- java GET和POST请求返回数据
- 利用fsocket模拟GET和POST请求
- curl 模拟get和post请求
- CURL 模拟get和post请求
- curl模拟get和post请求
- Oracle在.NET里面的DBHelper、OracleDBHelper
- fragment解析
- jdbcUtils
- C#和vb.net代码相互转换,翻译
- hdu2089:不要62
- .net 模拟GET或者POST请求 传参URL和获取返回值
- 替换
- 使用C#发送POST请求
- vb.net如何发送含双引号的字符串。转义双引号
- CDH impala hive hbase 整合
- VB.NET GET接口传送数据,获取返回值
- 我的兄弟姐妹
- Eclipse Java Build Path 配置
- JAVA 获取农历