1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- using WUtilitiesV01.Helper.ExceptionHelper;
- using WUtilitiesV01.WLogs;
- namespace Ips.Adc.QfCard.Cli
- {
- internal class HttpHelper
- {
- public static string Post(string ip, string typeVal, Dictionary<string, object> keyValuePairs)
- {
- try
- {
- ip = String.Format("http://{0}:8080/php/ADCard28712", ip);
- HttpWebRequest webrequest = WebRequest.Create(ip) as HttpWebRequest;
- webrequest.Method = "post";
- //webrequest.Headers.Add("Content-Type:application/x-www-form-urlencoded;charset=UTF-8");
- webrequest.ContentType = "application/x-www-form-urlencoded";
- var body = "type={0}&data={1}";
- if (keyValuePairs == null)
- keyValuePairs = new Dictionary<string, object>();
- var jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(keyValuePairs);
- WLog.Instance.Info(string.Format("Post:{0}\t{1}", ip, string.Format(body, typeVal, (jsonStr))));
- body = string.Format(body, typeVal, Uri.EscapeDataString(jsonStr));
- byte[] postdatabyte = Encoding.UTF8.GetBytes(body);
- webrequest.ContentLength = postdatabyte.Length;
- using (Stream stream = webrequest.GetRequestStream())
- {
- stream.Write(postdatabyte, 0, postdatabyte.Length);
- }
- using (var httpWebResponse = webrequest.GetResponse())
- using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
- {
- string ret = responseStream.ReadToEnd();
- return "Result:" + ret;
- }
- }
- catch (Exception e)
- {
- WLog.Instance.Error(e,"post结果");
- return "Error:" + e.Message;
- }
- }
- }
- }
|