您的位置:首页 > 编程语言 > C#

My Web Access Code in C#(Basic Post&Get method) / C#访问Web代码(基本Post和Get方法)

2008-01-24 22:57 761 查看

using System;


using System.Collections.Generic;


using System.Text;


using System.Net;


using System.IO;


using System.Reflection;


using System.Configuration;




namespace GnimNiy




...{


    class WebAccess




    ...{




        public static string GetData(string url, CookieContainer myCookieContainer)




        ...{


            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);


            myHttpWebRequest.CookieContainer = myCookieContainer;


            myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*";


            myHttpWebRequest.Headers.Add("Accept-Language:zh-cn");


            myHttpWebRequest.Headers.Add("Accept-Encoding:gzip, deflate");


            myHttpWebRequest.KeepAlive = true;


            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; KuGooSoft; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30; MAXTHON 2.0)";


            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();


            myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);


            Stream myResponseStream = myHttpWebResponse.GetResponseStream();


            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));


            string outdata = myStreamReader.ReadToEnd();


            myStreamReader.Close();


            myResponseStream.Close();


            return outdata;


        }




        public static string PostData(string url, string indata, CookieContainer myCookieContainer)




        ...{


            string outdata = "";


            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);


            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";


            //myHttpWebRequest.ContentLength = indata.Length;


            myHttpWebRequest.Method = "POST";


            myHttpWebRequest.CookieContainer = myCookieContainer;


            Stream myRequestStream = myHttpWebRequest.GetRequestStream();


            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));


            myStreamWriter.Write(indata);


            myStreamWriter.Close();


            myRequestStream.Close();


            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();


            myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);


            Stream myResponseStream = myHttpWebResponse.GetResponseStream();


            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));


            outdata = myStreamReader.ReadToEnd();


            myStreamReader.Close();


            myResponseStream.Close();


            return outdata;


        }




        public static bool SetAllowUnsafeHeaderParsing20()




        ...{


            //Get the assembly that contains the internal class


            Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));


            if (aNetAssembly != null)




            ...{


                //Use the assembly in order to get the internal type for the internal class


                Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");


                if (aSettingsType != null)




                ...{


                    //Use the internal static property to get an instance of the internal settings class.


                    //If the static instance isn't created allready the property will create it for us.


                    object anInstance = aSettingsType.InvokeMember("Section",




                      BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] ...{ });




                    if (anInstance != null)




                    ...{


                        //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not


                        FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);


                        if (aUseUnsafeHeaderParsing != null)




                        ...{


                            aUseUnsafeHeaderParsing.SetValue(anInstance, true);


                            return true;


                        }


                    }


                }


            }


            return false;


        }




    }


}



Here it is...Simple and useful...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# web basic access string null