c#中http如何post字符串
2个回答
展开全部
参数: url: 目标地址
data:要post的数据比如a=1&b=2
public string GetPostString(string url, string data)
{
try
{
byte[] postBytes = Encoding.GetEncoding("utf-8").GetBytes(data);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "text/html";
myRequest.ContentLength = postBytes.Length;
myRequest.Proxy = null;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(postBytes, 0, postBytes.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
string content = reader.ReadToEnd();
return content;
}
}
catch (System.Exception ex)
{
return ex.Message;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询