C# HttpWebRequest 发起 POST GET 请求

C# HttpWebRequest 发起 POST GET 请求

一、工具类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace sync
{
    public class HttpApiHelper
    {

        /// <summary>
        /// HttpGet
        /// </summary>
        /// <param name="url"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static int HttpGet(string url, string contenttype, string token, out string result)
        {
            result = "";
            try
            {
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Timeout = 3000; // 设置3秒超时
                wbRequest.Proxy = null;
                wbRequest.Method = "GET";
                wbRequest.ContentType = contenttype;

                // 令牌
                if (!token.Equals(""))
                {
                    wbRequest.Headers.Add("token", token);
                }

                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();

                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    //using (StreamReader sReader = new StreamReader(responseStream, System.Text.Encoding.Default))
                    using (StreamReader sReader = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception e)
            {
                result = e.Message;
                return -1; // 出现异常,函数的返回值为-1。
            }
            return 0;
        }

        /// <summary>
        /// HttpPost
        /// </summary>
        /// <param name="url"></param>
        /// <param name="sendData"></param>
        /// <param name="reslut"></param>
        /// <returns></returns>
        public static int HttpPost(string url, string sendData, string contenttype, string token, out string reslut)
        {
            reslut = "";
            try
            {
                byte[] data = System.Text.Encoding.UTF8.GetBytes(sendData);
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Proxy = null;
                wbRequest.Method = "POST";
                //wbRequest.ContentType = "application/json"; //"application/x-www-form-urlencoded
                wbRequest.ContentType = contenttype; // "application/x-www-form-urlencoded";
                wbRequest.ContentLength = data.Length;

                // 令牌
                if (!token.Equals(""))
                {
                    wbRequest.Headers.Add("token", token);
                }

                using (Stream wStream = wbRequest.GetRequestStream())
                {
                    wStream.Write(data, 0, data.Length);
                }

                // 获取响应
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        reslut = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception e)
            {
                reslut = e.Message;
                return -1; // 出现异常,函数的返回值为-1
            }
            return 0;
        }
    }
}

二、调用

string url_lh = "http://192.168.0.10:8080/api/openbill";
string token = "";
JObject obj_lh = new JObject();
obj_lh.Add("workbill_no", workbill_no);  // 作业线票号
obj_lh.Add("line_no", line_no);          // 行号
obj_lh.Add("inform_no", inform_no);      // 通知单号
               
string result_lh = "";

HttpApiHelper.HttpPost(url_lh, obj_lh.ToString(), "application/json", token, out result_lh);

tbLogLine.BeginInvoke(new Action(() => { tbLogLine.AppendText(result_lh + Environment.NewLine); }));

JObject obj = (JObject)JsonConvert.DeserializeObject(result_lh);

string code = obj["code"].ToString();

if (code.Equals("ok"))
{
    result = "0";
}
else if (code.Equals("-1"))
{
    result = "-1";
}

 

发表回复

您的电子邮箱地址不会被公开。