aspx.cs 模拟post发起请求
2017-05-31 21:44
351 查看
方法一:
formData = "type=100";formData += "&memo=" + reason;//拼接参数
string formUrl = "http://222.333.666.888:10000/appinterface.aspx";
//注意提交的编码 这边是需要改变的 这边默认的是Default:系统当前编码
byte[] postData = Encoding.UTF8.GetBytes(formData);
// 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
Encoding myEncoding = Encoding.UTF8;
request.Method = "POST";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.ContentLength = postData.Length;
// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
接收端:
用Request["type"].ToString()和Request["memo"].ToString()来接收对应的参数。
方法二:
借助第三方插件实现,先列使用方法,下边贴出插件源码,
NameValueCollection dataJB = new NameValueCollection();
string contents = "你辖区场所检查....";
dataJB.Add("type", "100");
dataJB.Add("loginname", userid);
dataJB.Add("contents", contents);
dataJB.Add("touser", policeid + ",");
dataJB.Add("urgenttype", "2");//默认为重要
DateTime enddate = DateTime.Now.AddDays(1);
dataJB.Add("enddate", enddate.ToString("yyyy-MM-dd HH:mm:ss"));
dataJB.Add("action", "saveassign");
HttpHelper.RedirectAndPOST(this.Page, "http://192.168.10.104:8003/System/Controls/appinterface.aspx", dataJB);
第二种方法存在个问题,如果使用ajax获取的话,返回结果里会有form标签,这是插件动态添加form标签导致的。
插件源码如下:
HttpHelper.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Collections.Specialized;
using System.Text;
namespace BLL
{
public static class HttpHelper
{
/// <summary>
/// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
/// </summary>
/// <param name="url">The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.</param>
/// <param name="data">A collection of data that will be posted to the destination Url.</param>
/// <returns>Returns a string representation of the Posting form.</returns>
/// <Author>Samer Abu Rabie</Author>
private static String PreparePOSTForm(string url, NameValueCollection data)
{
//Set a name for the form
string formID = "PostForm";
//Build the form using the specified data to be posted.
StringBuilder strForm = new StringBuilder();
strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">");
foreach (string key in data)
{
strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");
}
strForm.Append("</form>");
//Build the JavaScript which will do the Posting operation.
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var v" + formID + " = document." + formID + ";");
strScript.Append("v" + formID + ".submit();");
strScript.Append("</script>");
//Return the form and the script concatenated. (The order is important, Form then JavaScript)
return strForm.ToString() + strScript.ToString();
}
/// <summary>
/// POST data and Redirect to the specified url using the specified page.
/// </summary>
/// <param name="page">The page which will be the referrer page.</param>
/// <param name="destinationUrl">The destination Url to which the post and redirection is occuring.</param>
/// <param name="data">The data should be posted.</param>
/// <Author>Samer Abu Rabie</Author>
public static void RedirectAndPOST(Page page, string destinationUrl, NameValueCollection data)
{
//Prepare the Posting form
string strForm = PreparePOSTForm(destinationUrl, data);
//Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
page.Controls.Add(new LiteralControl(strForm));
}
}
}
formData = "type=100";formData += "&memo=" + reason;//拼接参数
string formUrl = "http://222.333.666.888:10000/appinterface.aspx";
//注意提交的编码 这边是需要改变的 这边默认的是Default:系统当前编码
byte[] postData = Encoding.UTF8.GetBytes(formData);
// 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
Encoding myEncoding = Encoding.UTF8;
request.Method = "POST";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.ContentLength = postData.Length;
// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
接收端:
用Request["type"].ToString()和Request["memo"].ToString()来接收对应的参数。
方法二:
借助第三方插件实现,先列使用方法,下边贴出插件源码,
NameValueCollection dataJB = new NameValueCollection();
string contents = "你辖区场所检查....";
dataJB.Add("type", "100");
dataJB.Add("loginname", userid);
dataJB.Add("contents", contents);
dataJB.Add("touser", policeid + ",");
dataJB.Add("urgenttype", "2");//默认为重要
DateTime enddate = DateTime.Now.AddDays(1);
dataJB.Add("enddate", enddate.ToString("yyyy-MM-dd HH:mm:ss"));
dataJB.Add("action", "saveassign");
HttpHelper.RedirectAndPOST(this.Page, "http://192.168.10.104:8003/System/Controls/appinterface.aspx", dataJB);
第二种方法存在个问题,如果使用ajax获取的话,返回结果里会有form标签,这是插件动态添加form标签导致的。
插件源码如下:
HttpHelper.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Collections.Specialized;
using System.Text;
namespace BLL
{
public static class HttpHelper
{
/// <summary>
/// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
/// </summary>
/// <param name="url">The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.</param>
/// <param name="data">A collection of data that will be posted to the destination Url.</param>
/// <returns>Returns a string representation of the Posting form.</returns>
/// <Author>Samer Abu Rabie</Author>
private static String PreparePOSTForm(string url, NameValueCollection data)
{
//Set a name for the form
string formID = "PostForm";
//Build the form using the specified data to be posted.
StringBuilder strForm = new StringBuilder();
strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">");
foreach (string key in data)
{
strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");
}
strForm.Append("</form>");
//Build the JavaScript which will do the Posting operation.
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var v" + formID + " = document." + formID + ";");
strScript.Append("v" + formID + ".submit();");
strScript.Append("</script>");
//Return the form and the script concatenated. (The order is important, Form then JavaScript)
return strForm.ToString() + strScript.ToString();
}
/// <summary>
/// POST data and Redirect to the specified url using the specified page.
/// </summary>
/// <param name="page">The page which will be the referrer page.</param>
/// <param name="destinationUrl">The destination Url to which the post and redirection is occuring.</param>
/// <param name="data">The data should be posted.</param>
/// <Author>Samer Abu Rabie</Author>
public static void RedirectAndPOST(Page page, string destinationUrl, NameValueCollection data)
{
//Prepare the Posting form
string strForm = PreparePOSTForm(destinationUrl, data);
//Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
page.Controls.Add(new LiteralControl(strForm));
}
}
}
相关文章推荐
- PostMan 模拟AJAX发起POST请求 SpringMVC
- 【转】C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- iOS 在线模拟post请求和get请求
- 一个模拟304机制的方法——针对一次超大数据的post请求
- php curl 模拟post 请求
- php模拟post提交请求,调用接口
- 模拟POST请求调用WCF
- java中使用scoket模拟http post请求发送图片或文件
- Java应用程序模拟向Servlet发送POST请求
- 使用urllib发起请求- urlopen发起请求read/decode/getcode/info/geturl-post请求抽屉网
- php模拟post提交请求,调用接口
- JAVA模拟HTTP post请求上传图片
- C#模拟http 发送post或get请求
- php通过CURL模拟post提交请求
- [轉]ASP模拟POST提交请求上传文件
- httpclient模拟post请求json封装表单数据
- php模拟发送GET和POST请求
- curl模拟post请求提交
- php模拟post提交请求,调用接口