Get与Post请求
程序员文章站
2022-04-15 18:07:14
...
Get请求:
/// <summary>
/// get请求
/// </summary>
/// <param name="uri">地址</param>
/// <param name="paramStr">参数</param>
/// <param name="token">令牌</param>
/// <returns></returns>
public static string WebClientRequestGet(string uri, string paramStr, string token = null)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri + paramStr); ;//创建请求
request.Method = "Get";//设置访问方式
request.ContentType = "application/json";
if (token != null)
{
request.Headers.Add("Authorization", "Bearer " + token);
}
try
{
System.Net.HttpWebResponse result = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(result.GetResponseStream(), Encoding.UTF8);
string strResult = sr.ReadToEnd();
sr.Close();
return strResult;
}
catch (Exception e)
{
//记录日志e.Message
//throw new Exception(e.Message);
return null;
}
}
Post请求:
/// <summary>
/// Post请求
/// </summary>
/// <param name="uri">地址</param>
/// <param name="paramStr">参数</param>
/// <param name="encoding">编码</param>
/// <param name="token">令牌</param>
/// <returns></returns>
public static string WebClientRequestPost(string uri, string paramStr, Encoding encoding, string token = null)
{
if (encoding == null)
encoding = Encoding.UTF8;
System.Net.WebClient wc = new System.Net.WebClient(); //WebClient wc = new WebClientto(600000);//设置600s超时时间
// 采取POST方式必须加的Header
wc.Headers.Add("Content-Type", "application/json");
if (token != null)
{
wc.Headers.Add("Authorization", "Bearer " + token);
}
byte[] postData = encoding.GetBytes(paramStr);
byte[] responseData = null;
try
{
responseData = wc.UploadData(uri, "POST", postData); // 得到返回字符流
return encoding.GetString(responseData);// 解码
}
catch (Exception e)
{
//记录日志e.Message
//throw new Exception(e.Message);
return null;
}
}
以上token都是在Header里的,还有token直接在链接后面的。其中System.Net.Webclient在下载请求时无法设置请求超时时间和请求读写超时时间。WebClient在异步下载时遇到网络不通等问题时没有响应超时造成app挂起,会造成请求超时。
解决方法:
1.写一个类继承Webclient,并且重写GetWebRequest,添加WebRequest对象超时时间 。
public class WebClientto : WebClient
{
/// <summary>
/// 过期时间
/// </summary>
public int Timeout { get; set; }
public WebClientto(int timeout)
{
Timeout = timeout;
}
/// <summary>
/// 重写GetWebRequest,添加WebRequest对象超时时间
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.Timeout = Timeout;
request.ReadWriteTimeout = Timeout;
return request;
}
}
2、直接使用HttpWebRequest
/// <summary>
/// Post请求
/// </summary>
/// <param name="uri">地址</param>
/// <param name="paramStr">参数</param>
/// <returns></returns>
public string RequestPost(string uri, string paramStr)
{
string strResult = string.Empty;
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri); ;//创建请求
request.Timeout = 600000; //请求超时前等待的毫秒数。默认值是 100,000 毫秒(100 秒)
request.ReadWriteTimeout = 600000; //获取或设置写入或读取流时的超时
request.Method = "POST";//设置访问方式
request.ContentType = "application/json";
#region 添加post参数
byte[] data = Encoding.UTF8.GetBytes(paramStr);
request.ContentLength = data.Length;
using (System.IO.Stream reqStream = request.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
#endregion
System.Net.HttpWebResponse result = request.GetResponse() as System.Net.HttpWebResponse;
//获取响应内容
using (System.IO.StreamReader sr = new System.IO.StreamReader(result.GetResponseStream(), Encoding.UTF8))
{
strResult = sr.ReadToEnd();
sr.Close();
return strResult;
}
}
catch (Exception ex) { throw new Exception(ex.Message); }
finally { strResult = string.Empty; }
}
推荐阅读
-
PHP中使用socket方式GET、POST数据实例_PHP
-
php fsockopen伪造post与get方法的详解
-
WordPress开发中的get_post_custom()函数使用解析,wordpressgetpost
-
wordpress获取自定义字段get_post_meta函数_MySQL
-
php函数get_headers是HEAD请求或GET请求
-
JS发送json格式POST请求有哪些方式
-
JS发送json格式POST请求
-
CI框架,源代码一次性判断获取post(get)数据是否有某个字段值为空方法,cipost
-
php模拟post请求发送文件_PHP教程
-
jquery ajax请求方式与提示用户正在处理请稍等