CookieContainer cookieContainer = new CookieContainer();
[HttpPost]
public string CommonPost(string url)
{
log.Info(CookieHelper.GetCookie("ITDC_UserName") + "进入方法CommonPost Url=" + url);
Uri address = new Uri(System.Configuration.ConfigurationManager.AppSettings["RESTfulAPI"].ToString() + url);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//远程服务,需要加入cookie验证
cookieContainer.Add(address,GetCookie("ITDC_UserName"));
cookieContainer.Add(address,GetCookie("ITDC_UserRole"));
request.CookieContainer = cookieContainer;
StringBuilder data = new StringBuilder();
for (int i = 0; i < Request.QueryString.Count; i++)
{
if (Request.QueryString.Keys[i].ToString() == "url") continue;
data.Append("&" + Request.QueryString.Keys[i].ToString() + "=" + Request.QueryString[i].ToString());
}
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString().TrimStart('&'));
// Set the content length in the request headers
request.ContentLength = byteData.Length;
// Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData,byteData.Length);
}
string result = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
}
log.Info(CookieHelper.GetCookie("ITDC_UserName") + " 执行完成 CommonPost Url=" + url);
return (result);
}