Http post提交和get提交

 public string  PostParameter(string url)
        {  string message="";
            GetOrderInfoMation order=new GetOrderInfoMation();
            string Parameter = "orderno=" + order.GetOrderNos() + "&receiptdata=test&result="status"="0"; "product_id"="2"; "transaction_id"="3"; "bid"="4"";
            byte[] bytes = Encoding.UTF8.GetBytes(Parameter);
            GetConfig con = new GetConfig();
            string prfix = con.UrlPreFix;//获取地址前缀
            string strURL = prfix + url;//创建一个訪问地址
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            request.ContentLength = bytes.Length;
            request.ContentType = "text/xml";
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string jsonstr = sr.ReadToEnd();
            return jsonstr;
            if (response.StatusCode != HttpStatusCode.OK)
            {
                return "POST failed. Received HTTP+" + response.StatusCode + "";
               
            }

        }

在url页面 处理请求 //接收receipt-data
            byte[] byts = new byte[Request.InputStream.Length];
            Request.InputStream.Read(byts, 0, byts.Length);
            sReceiptData = System.Text.Encoding.Default.GetString(byts);
            string[] Data = sReceiptData.Split('&');//获取參数




get请求

  public string Response(string url)
        {
            try
            {


                GetConfig con = new GetConfig();


                string prfix = con.UrlPreFix;//获取地址前缀
                string strURL = prfix + url;//创建一个訪问地址
                System.Net.HttpWebRequest request;
                request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);//创建一个HTTP请求
                request.Method = "get";//发送请求
                System.Net.HttpWebResponse response;
                response = (System.Net.HttpWebResponse)request.GetResponse();//获取响应
                StreamReader sr = new StreamReader(response.GetResponseStream());
                string jsonstr = sr.ReadToEnd();
                return jsonstr;
            }
            catch (Exception)
            {


                return "The server connection error.";
            }






        }



原文地址:https://www.cnblogs.com/liguangsunls/p/6949564.html