97 lines
3.2 KiB
C#
97 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Ewide.NbzsZheliban.Tools
|
|
{
|
|
public class HttpHelper
|
|
{
|
|
/// <summary>
|
|
/// 请求网页地址
|
|
/// </summary>
|
|
/// <param name="url">网页地址</param>
|
|
/// <param name="param">参数</param>
|
|
/// <returns></returns>
|
|
public static string CallUrl(string url, string param)
|
|
{
|
|
//创建一个HTTP请求
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
//Post请求方式
|
|
request.Method = "POST";
|
|
//内容类型
|
|
//request.ContentType = "application/x-www-form-urlencoded";
|
|
request.ContentType = "application/json;charset=utf-8";
|
|
//byte[] bs = Encoding.ASCII.GetBytes(param); //参数转化为ascii码
|
|
byte[] bs = Encoding.UTF8.GetBytes(param);
|
|
request.ContentLength = bs.Length;
|
|
using (Stream reqStream = request.GetRequestStream())
|
|
{
|
|
reqStream.Write(bs, 0, bs.Length);
|
|
}
|
|
|
|
String strValue = "";//strValue为http响应所返回的字符流
|
|
HttpWebResponse response;
|
|
try
|
|
{
|
|
//获得响应流
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
response = ex.Response as HttpWebResponse;
|
|
}
|
|
|
|
Stream s = response.GetResponseStream();
|
|
StreamReader sr = new StreamReader(s);
|
|
|
|
strValue = sr.ReadToEnd();
|
|
return strValue;
|
|
}
|
|
/// <summary>
|
|
/// 请求网页地址
|
|
/// </summary>
|
|
/// <param name="url">网页地址</param>
|
|
/// <param name="param">参数</param>
|
|
/// <returns></returns>
|
|
public static string CallUrl_xwwwformurlencoded(string url, string param)
|
|
{
|
|
//创建一个HTTP请求
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
//Post请求方式
|
|
request.Method = "POST";
|
|
//内容类型
|
|
request.ContentType = "application/x-www-form-urlencoded";
|
|
//request.ContentType = "application/json;charset=utf-8";
|
|
//byte[] bs = Encoding.ASCII.GetBytes(param); //参数转化为ascii码
|
|
byte[] bs = Encoding.UTF8.GetBytes(param);
|
|
request.ContentLength = bs.Length;
|
|
using (Stream reqStream = request.GetRequestStream())
|
|
{
|
|
reqStream.Write(bs, 0, bs.Length);
|
|
}
|
|
|
|
String strValue = "";//strValue为http响应所返回的字符流
|
|
HttpWebResponse response;
|
|
try
|
|
{
|
|
//获得响应流
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
response = ex.Response as HttpWebResponse;
|
|
}
|
|
|
|
Stream s = response.GetResponseStream();
|
|
StreamReader sr = new StreamReader(s);
|
|
|
|
strValue = sr.ReadToEnd();
|
|
return strValue;
|
|
}
|
|
}
|
|
}
|