C# 如何实现post提交数据
3个回答
展开全部
为什么不能表单提交?
可以利用System.Net.WebRequest对象提交。给你一段示例:
protected string RequestUrl(string url, string data = null, string method = "GET", string contentType = "text", string charset = "utf-8")
{
var request = WebRequest.Create(url);
request.Method = method;
request.ContentType = contentType;
request.Headers.Add("charset:" + charset);
var encoding = Encoding.GetEncoding(charset);
if (data != null)
{
byte[] buffer = encoding.GetBytes(data);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
}
else
{
request.ContentLength = 0;
}
using (HttpWebResponse wr = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(wr.GetResponseStream(), encoding))
{
return reader.ReadToEnd();
}
}
}
可以利用System.Net.WebRequest对象提交。给你一段示例:
protected string RequestUrl(string url, string data = null, string method = "GET", string contentType = "text", string charset = "utf-8")
{
var request = WebRequest.Create(url);
request.Method = method;
request.ContentType = contentType;
request.Headers.Add("charset:" + charset);
var encoding = Encoding.GetEncoding(charset);
if (data != null)
{
byte[] buffer = encoding.GetBytes(data);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
}
else
{
request.ContentLength = 0;
}
using (HttpWebResponse wr = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(wr.GetResponseStream(), encoding))
{
return reader.ReadToEnd();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你百度一下 HttpWebRequest
可以透过它来模拟提交动作
可以透过它来模拟提交动作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
提交什么类型的数据!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询