一.RestSharp(需要引入RestSharp.dll 文件)
/ // <summary> /// </summary> /// <param name="uploadData">x</param> /// <param name="strFlag"></param> public void Upload(string uploadData,string upload_IP) { try { var client = new RestSharp.RestClient(upload_IP); var requesPost = new RestRequest("", Method.POST); requesPost.AddParameter("application/x-www-form-urlencoded", uploadData, ParameterType.RequestBody); IRestResponse responsePost = client.Execute(requesPost); var responseContent = responsePost.Content; } catch (Exception ex) { } }
二.
/// <summary> ///上传的格式为json字符串 /// </summary> /// <param name="postUrl">请求地址</param> /// <param name="paramData">入参</param> /// <param name="dataEncode">编码方式</param> /// <returns></returns> public string PostWebRequest(string postUrl, string paramData, Encoding dataEncode) { string ret = string.Empty; try { byte[] byteArray = dataEncode.GetBytes(paramData); //转化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/json"; webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { MessageBoxs.ShowError(ex.Message); } return ret; }