Httpost带cookie请求

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication9
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //data
            string cookieStr = "JSESSIONID=5F3EE40FA996A2CF5DD456C73115D64C";
            string postData = string.Format("searchValue={0}&page={0}&params[orgId]={2}", "%", "12", "foshan");
            byte[] data = Encoding.UTF8.GetBytes(postData);

            // Prepare web request...
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://jiachi.weyaocn.com/api/customer/list");
            request.Method = "POST";
            //request.Referer = "https://www.xxx.com";
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
            //request.Host = "www.xxx.com";
            request.Headers.Add("Cookie", cookieStr);           
            request.ContentLength = data.Length;
            Stream newStream = request.GetRequestStream();

            // Send the data.
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            // Get response
            HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            string content = reader.ReadToEnd();
            Console.WriteLine(content);
            Console.ReadLine();

        }

    }
}

 

posted @ 2019-04-12 15:53  Dukezhou  阅读(101)  评论(0)    收藏  举报