C#转发Post请求,包括参数和⽂件
///<summary>
param name///转发Post请求
///</summary>
///<param name="curRequest">要转发的请求</param>
///<param name="url">转发到的Url地址</param>
public static string ForwardRequest(HttpRequest curRequest, string url)
{
byte[] inputBytes;
int inputStreamLength;
using (var inputStream = curRequest.InputStream)
{
inputStreamLength = Convert.ToInt32(inputStream.Length);
inputBytes = new byte[inputStreamLength + 1];
inputStream.Read(inputBytes, 0, inputStreamLength);
inputStream.Close();
}
//构造请求
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = curRequest.ContentType;
request.ContentLength = curRequest.ContentLength;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(inputBytes, 0, inputStreamLength);
requestStream.Close();
}
string result = string.Empty;
using (WebResponse response = request.GetResponse())
{
if (response != null)
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
}
}
return result;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论