
正文
模拟远程HTTP的POST请求
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
建立请求,以模拟远程HTTP的POST请求方式构造并获取处理结果
/// <summary>
/// 建立请求,以模拟远程HTTP的POST请求方式构造并获取处理结果
/// </summary>
/// <param name="sParaTemp">请求参数数组</param>
/// <returns>处理结果</returns>
public string BuildRequest(Dictionary<string, string> sParaTemp,string url,string input_chartset)
{
Encoding code = Encoding.GetEncoding(input_chartset); //待请求参数数组字符串
string strRequestData = BuildRequestParaToString(sParaTemp, code); //把数组转换成流中所需字节数组类型
byte[] bytesRequestData = code.GetBytes(strRequestData); //构造请求地址
string strUrl = url; //请求远程HTTP
string strResult = "";
try
{
//设置HttpWebRequest基本信息
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
myReq.Method = "post";
myReq.ContentType = "application/x-www-form-urlencoded"; //填充POST数据
myReq.ContentLength = bytesRequestData.Length;
Stream requestStream = myReq.GetRequestStream();
requestStream.Write(bytesRequestData, , bytesRequestData.Length);
requestStream.Close(); //发送POST数据请求服务器
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream(); //获取服务器返回信息
StreamReader reader = new StreamReader(myStream, code);
StringBuilder responseData = new StringBuilder();
String line;
while ((line = reader.ReadLine()) != null)
{
responseData.Append(line);
} //释放
myStream.Close(); strResult = responseData.ToString();
}
catch (Exception exp)
{
strResult = "报错:" + exp.Message;
} return strResult;
} /// <summary>
/// 生成要请求的参数数组
/// </summary>
/// <param name="sParaTemp">请求前的参数数组</param>
/// <param name="code">字符编码</param>
/// <returns>要请求的参数数组字符串</returns>
private string BuildRequestParaToString(Dictionary<string, string> sParaTemp, Encoding code)
{
//把参数组中所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
string strRequestData = CreateLinkStringUrlencode(sParaTemp, code);
return strRequestData;
} /// <summary>
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
/// </summary>
/// <param name="sArray">需要拼接的数组</param>
/// <param name="code">字符编码</param>
/// <returns>拼接完成以后的字符串</returns>
private string CreateLinkStringUrlencode(Dictionary<string, string> dicArray, Encoding code)
{
StringBuilder prestr = new StringBuilder();
foreach (KeyValuePair<string, string> temp in dicArray)
{
prestr.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value, code) + "&");
} //去掉最後一個&字符
int nLen = prestr.Length;
prestr.Remove(nLen - , ); return prestr.ToString();
}
调用
/// <summary>
/// 查询电子邮件状态
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult QueryEmailStatus()
{
string responseText = string.Empty;
try
{
#region 调用查询电子邮件状态接口 string url = "http://localhost/WebAPI/status.aspx";
string input_chartset="utf-8";
//构造参数
Dictionary<string, string> sParaTemp = new Dictionary<string, string>();
sParaTemp.Add("ManagerLoginName", "XXXXXXX");//API帳號
sParaTemp.Add("ManagerPassword", "XXXXXXX");//API密碼 sParaTemp.Add("JobType", "");//1:簡訊 2:電子郵件
sParaTemp.Add("LaunchID", "");//任務編號
sParaTemp.Add("EmailAddress", "394401333@qq.com");//查詢的對象(收件者信箱) //提交请求,获得返回结果
responseText = BuildRequest(sParaTemp, url, input_chartset); #endregion
}
catch (Exception ex)
{
throw ex;
//Log.Error(this.GetType().ToString(), "Exception: " + ex.Message);
}
return RedirectToAction("Index", "Demo");
}






