您的位置:68399皇家赌场 > 服务器租用 > 【www.68399.com】WinForm POST上传与后台接受

【www.68399.com】WinForm POST上传与后台接受

发布时间:2019-05-31 20:13编辑:服务器租用浏览(179)

    那其间,正则表明式迷糊了自己说话:因为未有选用惰性相称,导致每二次都只好合营到一条音讯。。。。

    C# 模拟Http/Https请求框架类,

    using System.Text;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Security.Cryptography.X509Certificates;
    using System.Net.Security;

    namespace WebRequestTest
    {
        /// <summary>
        /// 动态类,各样实例使用单独session
        /// </summary>
        public class HttpHelperNew
        {
           public CookieContainer cookie = new CookieContainer();
            /// <summary>
            /// post请求重回html
            /// </summary>
            /// <param name="Url"></param>
            /// <param name="postDataStr"></param>
            /// <returns></returns>
            public string HttpPost(string Url, string postDataStr)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                //request.AllowAutoRedirect = false; //禁止自动重定向
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
                request.CookieContainer = cookie; //cookie新闻由CookieContainer自行维护
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();

                HttpWebResponse response = null;
                try
                {
                    this.SetCertificatePolicy();
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (System.Exception ex)
                {
                }
                //获取重定向地址
                //string url1 = response.Headers["Location"];
                if (response !=null)
                {
                    Stream myResponseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                    string retString = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                    myResponseStream.Close();
                    return retString;
                }
                else
                {
                    return "error"; //post请求重临为空
                }

            }

            /// <summary>
            /// get请求获取再次来到的html
            /// </summary>
            /// <param name="Url"></param>
            /// <param name="postDataStr"></param>
            /// <returns></returns>
            public string HttpGet(string Url, string Querydata)
            {
               
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url (Querydata == "" ? "" : "?") Querydata);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                request.CookieContainer = cookie;
                this.SetCertificatePolicy();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               // response.Cookies = cookie.GetCookies(response.ResponseUri);
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }

            /// <summary>
            /// 得到响应中的图像
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public Stream GetResponseImage(string url)
            {
                Stream resst = null;
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    req.KeepAlive = true;
                    req.Method = "GET";
                    req.AllowAutoRedirect = true;
                    req.CookieContainer = cookie;
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.Accept = "text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8";
                    req.Timeout = 50000;

                    Encoding myEncoding = Encoding.GetEncoding("UTF-8");
                    this.SetCertificatePolicy();
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    resst = res.GetResponseStream();

                    return resst;
                }
                catch
                {
                    return null;
                }
            }

            /// <summary>
            /// 正则获取相称的第二个值
            /// </summary>
            /// <param name="html"></param>
            /// <param name="pattern"></param>
            /// <returns></returns>
            public string getStringByRegex(string html,string pattern)
            {

                Regex re = new Regex(pattern, RegexOptions.IgnoreCase);
                MatchCollection matchs = re.Matches(html);
                if (matchs.Count > 0)
                {
                    return matchs[0].Groups[1].Value;
                }
                else
                    return "";
            }

            /// <summary>
            /// 正则验证重回的response是还是不是正确
            /// </summary>
            /// <param name="html"></param>
            /// <param name="pattern"></param>
            /// <returns></returns>
            public bool verifyResponseHtml(string html ,string pattern)
            {
                Regex re = new Regex(pattern);
                return re.IsMatch(html);
            }

            //注册证件验证回调事件,在伸手从前注册
            private void SetCertificatePolicy()
            {
                ServicePointManager.ServerCertificateValidationCallback
                           = RemoteCertificateValidate;
            }
            /// <summary>  
            /// 远程证书验证,固定再次来到true
            /// </summary>  
            private static bool RemoteCertificateValidate(object sender, X509Certificate cert,
                X509Chain chain, SslPolicyErrors error)
            {
                return true;
            }  

        }
    }

    参照他事他说加以调查页面:

    模拟Http/Https请求框架类, using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; using System.Security.Cryptography.X50玖Certificate...

    postStream.Close();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "错误");
    }

                HttpWebResponse response = null;
                try
                {
                    this.SetCertificatePolicy();
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (System.Exception ex)
                {
                }
                //获取重定向地址
                //string url1 = response.Headers["Location"];
                if (response !=null)
                {
                    Stream myResponseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                    string retString = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                    myResponseStream.Close();
                    return retString;
                }
                else
                {
                    return "error"; //post请求重返为空
                }

          

    namespace WinFormPOST
    {
    public partial class UP : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    HttpPostedFile f;
    if (Request.Files.Count > 0)
    {
    for (int i = 0; i < Request.Files.Count; i )
    {
    f = Request.Files[i];
    string fName = Request.QueryString["fname"];
    if (string.IsNullOrEmpty == false)
    f.SaveAs(Server.MapPath("~/UploadFiles/") fName);
    else
    {
    Random rnd = new Random();
    f.SaveAs(Server.MapPath("~/UploadFiles/") rnd.Next "_002.jpg");
    }
    }

    using System.Text;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Security.Cryptography.X509Certificates;
    using System.Net.Security;

     当中使用了HttpWebRequest和正则表明式,代码备忘如下:  

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

            /// <summary>
            /// 获得响应中的图像
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public Stream GetResponseImage(string url)
            {
                Stream resst = null;
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    req.KeepAlive = true;
                    req.Method = "GET";
                    req.AllowAutoRedirect = true;
                    req.CookieContainer = cookie;
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.Accept = "text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8";
                    req.Timeout = 50000;

     最近做了一下搜集有个别网页的从头到尾的经过,并得到个中有着的链接地址及链接标题。

    try
    {
    byte[] postArray = r.ReadBytesfs.Length);
    Stream postStream = myWebClient.OpenWrite;
    // postStream.m
    if (postStream.CanWrite)
    {
    postStream.Write(postArray, 0, postArray.Length);
    MessageBox.Show("文件上传成功!", "提醒", Message博克斯Buttons.OK, MessageBoxIcon.Information);
    }
    else
    {
    MessageBox.Show("文件上传错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }

                    return resst;
                }
                catch
                {
                    return null;
                }
            }

    www.68399.com, 

    private void button5_Click(object sender, EventArgs e)
    {
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.InitialDirectory = "c:\";//注意这里写路线时要用c:\而不是c:
    openFileDialog1.Filter = "图片|*.jpg|PNG|*.png|全体文件|*.*";
    openFileDialog1.RestoreDirectory = true;
    openFileDialog1.FilterIndex = 1;
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    try
    {
    string path = openFileDialog1.FileName;
    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;
    wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    // wc.Headers.Add("Content-Type", "multipart/form-data");
    //wc.QueryString["fname"] = openFileDialog1.SafeFileName;//没用
    //MessageBox.Show(openFileDialog1.SafeFileName);
    byte[] fileb = wc.UploadFile(new Uri(@"" openFileDialog1.SafeFileName), "POST", path);
    string res = Encoding.GetEncoding.GetString;
    wc.Dispose();
    MessageBox.Show;
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

            }

    www.68399.com 1 //WebClient wc = new WebClient();
    www.68399.com 2//NetworkCredential nc = new NetworkCredential("用户名", "密码", "域名");
    www.68399.com 3        //wc.Credentials = nc;
    www.68399.com 4        //Response.Write(Server.HtmlEncode(wc.DownloadString("地址")));
    www.68399.com 5
    www.68399.com 6        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("地址");
    www.68399.com 7        req.Credentials = new NetworkCredential("用户名", "密码", "域名");
    www.68399.com 8        req.Method = "GET";        
    www.68399.com 9        IAsyncResult ir = req.BeginGetResponse(null, null);
    www.68399.com 10        ir.AsyncWaitHandle.WaitOne();
    www.68399.com 11www.68399.com 12        try www.68399.com 13{
    www.68399.com 14            HttpWebResponse response1 = (HttpWebResponse)req.EndGetResponse(ir);
    www.68399.com 15            System.IO.Stream stream = response1.GetResponseStream();
    www.68399.com 16            sReader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("GB2312"));
    www.68399.com 17www.68399.com 18            if (null != sReader) www.68399.com 19{
    www.68399.com 20            string pattern = @"<a(?:s*?)href=['|""](?<url>[sS] ?)['|""]>(?<title>[sS] ?)</a>";
    www.68399.com 21            System.Text.RegularExpressions.MatchCollection matchs = System.Text.RegularExpressions.Regex.Matches(sReader.ReadToEnd(), pattern);
    www.68399.com 22            if (matchs.Count <= 0)
    www.68399.com 23                Response.Write("没有相配项");
    www.68399.com 24            else
    www.68399.com 25www.68399.com 26            www.68399.com 27{
    www.68399.com 28                for(int i=0;i<50;i )
    www.68399.com 29www.68399.com 30                www.68399.com 31{
    www.68399.com 32                    Response.Write("链接:"   matchs[i].Groups["url"].Value "___名称:" matchs[i].Groups["title"].Value "<br />");
    www.68399.com 33                }
    www.68399.com 34            }
    www.68399.com 35            }
    www.68399.com 36        }
    www.68399.com 37www.68399.com 38        catch (System.Exception ex) www.68399.com 39{
    www.68399.com 40            Response.Write(ex.Message);
    www.68399.com 41        }
    www.68399.com 42www.68399.com 43        finally www.68399.com 44{
    www.68399.com 45www.68399.com 46            if (null != sReader) www.68399.com 47{
    www.68399.com 48                sReader.Dispose();
    www.68399.com 49            }
    www.68399.com 50        }

    urlPath = urlPath newFileName;

            /// <summary>
            /// get请求获取重回的html
            /// </summary>
            /// <param name="Url"></param>
            /// <param name="postDataStr"></param>
            /// <returns></returns>
            public string HttpGet(string Url, string Querydata)
            {
               
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url (Querydata == "" ? "" : "?") Querydata);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                request.CookieContainer = cookie;
                this.SetCertificatePolicy();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               // response.Cookies = cookie.GetCookies(response.ResponseUri);
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }

     

    前端

    namespace WebRequestTest
    {
        /// <summary>
        /// 动态类,每一种实例使用单独session
        /// </summary>
        public class HttpHelperNew
        {
           public CookieContainer cookie = new CookieContainer();
            /// <summary>
            /// post请求再次来到html
            /// </summary>
            /// <param name="Url"></param>
            /// <param name="postDataStr"></param>
            /// <returns></returns>
            public string HttpPost(string Url, string postDataStr)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                //request.AllowAutoRedirect = false; //禁止自动重定向
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
                request.CookieContainer = cookie; //cookie信息由库克ieContainer自行维护
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();

    本文由68399皇家赌场发布于服务器租用,转载请注明出处:【www.68399.com】WinForm POST上传与后台接受

    关键词: 68399皇家赌场 后台 上传 WinForm C# 模拟Http请求

上一篇:vue-lazyload图片延迟加载插件的实例疏解,

下一篇:没有了