using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace TestCon
{
static class InfoGeter
{
//静态方法传递两个参数,Post_String是单纯的链接,Post_Data是post的参数用以请求
public static string Info_Geter_Post_Get(string Post_String,string Post_Data) {
try {
string s_Connection_String = Post_String;
//实例化一个HttpWebrequst对象
HttpWebRequest Info_Request = (HttpWebRequest)WebRequest.Create(s_Connection_String);
//设置Requst的模式
Info_Request.Method = "POST";
//设置Content-Type Http标头的值,该值为默认值
Info_Request.ContentType = "application/x-www-form-urlencoded";
//预设响应等待时间
Info_Request.Timeout = 20000;
//建立一个Stream对象来写入Requst请求的参数流内涵url和key值等
Stream Info_Stream = Info_Request.GetRequestStream();
//调用Write方法第一个参数是获取传递参数的值得类型,第二个是流起始位置,第三个参数指流的长度
Info_Stream.Write(Encoding.UTF8.GetBytes(Post_Data), 0, Encoding.UTF8.GetBytes(Post_Data).Length);
//实例化一个HttpWebResponse用GetResponse方法用以获取服务器返回的响应
HttpWebResponse Info_Response = (HttpWebResponse)Info_Request.GetResponse();
//实例化一个StreamReader对象来获取Response的GetResponseStream返回的响应的体
StreamReader Info_Reader = new StreamReader(Info_Response.GetResponseStream(), Encoding.UTF8);
return Info_Reader.ReadToEnd()+"";
}
catch (System.Exception ex) {
Console.WriteLine(ex);
Console.ReadLine();
return "";
}
}
}
}
C#利用HttpWebRequest,Stream,HttpWebResponse,StreamReader获取post返回的数据
最新推荐文章于 2022-12-11 12:46:05 发布