///
/// 模拟请求
///
///
///
///
///
public string HttpApi(string url, string type, NameValueCollection values)
{
string result = "";
using (var client = new WebClient())
{
var response = client.UploadValues(url, type, values);
var responseString = Encoding.UTF8.GetString(response);
result = responseString.ToString();
}
return result;
}
public string HttpGet(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
调用方式:
string param = jsArray.GetValue(2).StringValue;//参数
NameValueCollection values = new NameValueCollection();
if (param.Trim() != "")
{
string[] arry = param.Split('★');
for (int i = 0; i < arry.Length; i++)
{
var arry2 = arry[i].Split('▲');
values[arry2[0]] = arry2[1];
bc.SetJsonItem("APIPARAM_" + arry2[0], arry2[1]);
}
}
string jsonData = bc.HttpApi(url, type, values);