获取http请求

    public  string GetUrlDate(string url, string method, string content)
    {
        string outstring = "";
        try
        {
            System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
            myRequest.Method = method;
            myRequest.KeepAlive = false;
            myRequest.ContentType = "application/x-www-form-urlencoded";

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            string postData = content;
            byte[] data = encoding.GetBytes(postData);
            myRequest.ContentLength = data.Length;
            System.IO.Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();


            using (System.Net.HttpWebResponse myResponse = (System.Net.HttpWebResponse)myRequest.GetResponse())
            {

                System.IO.StreamReader sr = new System.IO.StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                outstring= sr.ReadToEnd();
            }
        }
        catch (Exception ex)
        {
            IExceptionPublisher Publisher = new ExceptionPublisher();
            Publisher.Publish(ex, null, null);
            outstring= "";
        }
        return outstring;
    }

posted on 2010-08-31 23:16  linzheng  阅读(1090)  评论(0编辑  收藏  举报

导航