using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
/// <summary>
/// 带参的POST请求,传递文本数据
/// </summary>
/// <param name="url">例如 192.168.1.222:8080/getMaterialsBySpacePlanIdToClient</param>
/// <param name="parameters"></param>
/// <returns></returns>
public string HttpPostRequest(string url, string paramData)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf8"; // 必须指明字符集!
httpWebRequest.Timeout = 20000;
// 参数
// 给文本数据编码
byte[] data = Encoding.UTF8.GetBytes(paramData); // 必须与ContentType中指定的字符集一致!
// 往请求的流里写数据
using (Stream stream = httpWebRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
// 从响应对象中获取数据
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
string responseContent = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
httpWebRequest.Abort();
return responseContent;
}
调用方式
string postUrl = "http://open.wangpos.com/wopengateway/api/entry";
string paramdata = @"access_token=" + accessToken + "&nonce=" + _nonce + "&service="
+ WangPosMsgService + "&signature=" + _signature + "×tamp=" +
_timestamp + "&device_en=" + device_en + "&biz_code=" + WangPosBiz_code
+ "&data=" + strTuisong.ToString();
TKF.Common.showinfo.ShowInfo("推送的字符串===" + paramdata, true);
string MsgJson = HttpPostRequest(postUrl, paramdata);
TKF.Common.showinfo.ShowInfo("推送返回值MsgJson===" + MsgJson, true);
JObject joMsg = JObject.Parse(MsgJson);
string MsgStatus = joMsg["status"].ToString();
string Msg = joMsg["info"].ToString();
TKF.Common.showinfo.ShowInfo("MsgStatus===" + MsgStatus, true);
if (MsgStatus.Trim() == "0")
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "tkf.Alert('已经完成,请付款')", true);
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "tkf.Alert('" + Msg + "')", true);
}