zoukankan      html  css  js  c++  java
  • HttpWebRequest提交数据

    private string WebRequestMethod(string RequestUrl)
        {
            string str = "";
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                
                request = (HttpWebRequest) WebRequest.Create(RequestUrl);
                request.CookieContainer = this.Cookie;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
                response = (HttpWebResponse) request.GetResponse();
                reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                str = reader.ReadToEnd();
                reader.Close();
                response.Close();
            }
            catch (Exception exception)
            { 
                return "";
            }
            return str.Trim();
        }
    
        private string RequestPostMethod(string Post_Url, string PostData)
        {
            string str = "";
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            byte[] bytes = new ASCIIEncoding().GetBytes(PostData);
            try
            {
                request = (HttpWebRequest) WebRequest.Create(Post_Url);
                request.CookieContainer = this.Cookie;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = bytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Flush();
                requestStream.Close();
                response = (HttpWebResponse) request.GetResponse();
                reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                str = reader.ReadToEnd();
                reader.Close();
                response.Close();
            }
            catch (Exception exception)
            {
                return "";
            }
            return str.Trim();
        }
  • 相关阅读:
    POJ 1862 Stripies (简单贪心)
    HDU 2181 哈密顿绕行世界问题(DFS)
    POJ 3262 Protecting the Flowers (贪心)
    Aizu 2249 Road Construction (最短路 SPFA)
    POJ 3484 Showstopper(二分)
    HDU 6235 2017-CCPC-哈尔滨站 Permutation(简单模拟)
    IDEA常用插件
    解决IDEA插件加载失败
    解决zsh无法使用maven
    Linux安装Maven
  • 原文地址:https://www.cnblogs.com/ericyuan/p/3242850.html
Copyright © 2011-2022 走看看