프로그래밍언어/C#

C#용 httppost함수

부산딸랑이 2014. 8. 24. 15:15

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;


namespace WindowsFormsApplication1
{
    class Class1
    {
        public static CookieContainer cookie = new CookieContainer();
        public string OpenURL(string URL, string Method = "GET", string PostData = null)
        {

            string functionReturnValue = null;

            HttpWebRequest request = null;

            request = (HttpWebRequest)WebRequest.Create(URL);



            var _with1 = request;



            _with1.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";

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

            _with1.Accept = "*/*";


            _with1.KeepAlive = true;

            _with1.ReadWriteTimeout = -1;

            _with1.Timeout = -1;

            _with1.Method = Method;

            // get,post등을 설정

            _with1.CookieContainer = cookie;

            if (Method == "POST")
            {




                TextWriter w = new StreamWriter(_with1.GetRequestStream());

                w.Write(PostData);

                w.Close();

                w = null;

            }




            HttpWebResponse Response = (HttpWebResponse)request.GetResponse();

            var _with2 = Response;

            if (_with2.StatusCode == HttpStatusCode.OK)
            {

                Stream receivestream = _with2.GetResponseStream();

                StreamReader readstream = null;

                readstream = new StreamReader(receivestream, System.Text.Encoding.Default);

                functionReturnValue = readstream.ReadToEnd();

                readstream.Close();

                _with2.Close();

            }

            return functionReturnValue;

        }

    }
}

'프로그래밍언어 > C#' 카테고리의 다른 글

문자열 저장,불러오기 함수  (0) 2014.08.27
원클릭출첵  (0) 2014.08.24
Xamarin Toast메세지  (0) 2014.08.24
xamarin 값 저장  (0) 2014.08.24
C# 소켓통신  (0) 2014.06.16