/// <summary>
/// 上传的文件
/// </summary>
/// <param name="url">服务器请求地址</param>
/// <param name="paramDate">发送的数据</param>
/// <param name="encode">网站编码</param>
/// <returns>字符串类型的true/false</returns>
public static string PostMoth(string url, string paramDate, Encoding encode)
{
string str=string .Empty;
try
{
byte[] byteArray = encode.GetBytes(paramDate);//将数据转换成对应的网站编码
GC.Collect();
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);//发出请求
//ServicePointManager.DefaultConnectionLimit = 100;
webRequest.ContentType = "application/x-www-form-urlencoded";//报表头类型
webRequest.Method = "POST";//请求方式Post
webRequest.Timeout = 12000;//请求的超时时间
webRequest.ContentLength = byteArray.Length;//获取要传输的长度
Stream requeststream = webRequest.GetRequestStream();//获取写入的数据流
//StreamWriter strw = new StreamWriter(requeststream);
requeststream.Write(byteArray,0,byteArray .Length);//将数据写入到当前流中
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();//返回来自服务器的相应
if (webResponse.StatusCode == HttpStatusCode.OK)
{
if (webResponse.Headers["Content-Encoding"] != null && webResponse.Headers["Content-Encoding"].ToLower().Contains("gzip"))//如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可
{
requeststream = new GZipStream(requeststream, CompressionMode.Decompress);//解压缩基础流
}
StreamReader strReader = new StreamReader(webResponse.GetResponseStream(), Encoding .Default );//读取对应编码的文件流
str = strReader.ReadToEnd();//读文件流至最后
webResponse.Close();
strReader.Close();
requeststream.Close();//关闭文件流
return "true";
}
else
{
return "false";
}
}
catch (Exception e)
{
return e.ToString ();
}
}
}
(HttpWebRequest )服务器上传请求Post方式
最新推荐文章于 2025-02-27 00:09:48 发布