本文主要是介绍C#HttpWebRequest发布信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/// <summary>From:www.subeiwang.com
/// 远程发布数据
/// </summary>
public string RemoteSendHttpData(CookieContainer myCookieContainer, string Url, string PostDate, string Gbcode, string refUrl, HttpWebRequest myHttp)
{
if (string.IsNullOrEmpty(Gbcode))
{
Gbcode = "gb2312";
}
if (!string.IsNullOrEmpty(Url) && !string.IsNullOrEmpty(PostDate))
{
try
{
//CookieContainer myCookieContainer = new CookieContainer();
myHttp = (HttpWebRequest)HttpWebRequest.Create(Url);
myHttp.CookieContainer = myCookieContainer;//*发送COOKIE
myHttp.Method = "POST";
myHttp.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*";
myHttp.ContentType = "application/x-www-form-urlencoded";
myHttp.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36";
if (!string.IsNullOrEmpty(refUrl))
{
myHttp.Referer = refUrl;
}
else
{
myHttp.Referer = Url;
}
myHttp.KeepAlive = true;
myHttp.ProtocolVersion = HttpVersion.Version10;
myHttp.CookieContainer = myCookieContainer;
//myHttp.Headers.Add("x-requested-with", "XMLHttpRequest");
string postdata = PostDate;
byte[] byte1 = Encoding.GetEncoding(Gbcode).GetBytes(postdata);
myHttp.ContentLength = byte1.Length;
Stream poststream = myHttp.GetRequestStream();
poststream.Write(byte1, 0, byte1.Length);
poststream.Close();
HttpWebResponse hres = (HttpWebResponse)myHttp.GetResponse();
Stream stream = hres.GetResponseStream();
string WebContent = new StreamReader(stream, System.Text.Encoding.GetEncoding(Gbcode)).ReadToEnd();
stream.Close();
if (hres != null)
{
hres.Close();
}
if (myHttp != null)
{
myHttp.Abort();
myHttp = null;
}
return WebContent;
//return "ok";
}
catch (Exception ex)
{
//MessageBox.Show("错误信息:" + ex.Message.ToString(), "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
return "错误信息:" + ex.Message.ToString();
//return;
}
//finally
//{
// lb.Text = "总共发送:"+sendnum+"次,成功发送:" + sendok + "次,失败发送:" + sendfalt+"次";
//}
}
else
{
//MessageBox.Show("错误提交:提交链接或者数据为空", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
//return;
return "错误信息:提交链接或者数据为空";
}
}
这篇关于C#HttpWebRequest发布信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!