当前位置 : 主页 > 网络编程 > net编程 >

C# HTTP POST AND GET json or xml

来源:互联网 收集:自由互联 发布时间:2023-09-06
using System.Net; using System.Net.Cache; using System.IO; string HttpPost(string strUrl, string strPostData) { string result = string.Empty; try { HttpWebRequest request = HttpWebRequest.Create(strUrl) as HttpWebRequest; request.ContentTyp
        using System.Net;
using System.Net.Cache;
using System.IO;

string HttpPost(string strUrl, string strPostData)
{
string result = string.Empty;
try
{
HttpWebRequest request = HttpWebRequest.Create(strUrl) as HttpWebRequest;
request.ContentType = "application/json";//or application/xml
request.Method = "POST";
request.Timeout = 5000;
request.Accept = "*/*";
request.KeepAlive = true;
request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

byte[] bytes = Encoding.UTF8.GetBytes(strPostData);
request.ContentLength = bytes.Length;
Stream writer = request.GetRequestStream();
writer.Write(bytes, 0, bytes.Length);
writer.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
response.Close();

}
catch (Exception ex)
{
MessageBox.Show(new Form { TopMost = true }, ex.Message);
}

return result;
}
【文章出处:日本大带宽服务器 http://www.558idc.com/jap.html 欢迎留下您的宝贵建议】
上一篇:Where.FirstOrDefault和FirstOrDefault的性能对比
下一篇:没有了
网友评论