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

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, nullnullnew 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...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值