C# GetAsyncDataFromWeb

C#/통신 2016. 8. 21. 23:36
728x90

HttpWebRequest 방식으로는 데이터를 가져오는 크기에 한도가 있는 거 같다.

아래 방식으로 하면 데이터를 많이 가져온다.


// GET 방식으로 Web 에 있는 데이터를 가져온다.

void GetAsyncDataFromWeb(string url)
{
    var client = new HttpClient();
    var response = client.GetAsync(url).Result;
    string resultContent = response.Content.ReadAsStringAsync().Result;
}



// POST 방식으로 Web 에 있는 데이터를 가져온다.

void GetAsyncDataFromWeb(string url)
{
    var client = new HttpClient();
    var postData = new List<KeyValuePair<string, string>>();
    postData.Add(new KeyValuePair<string, string>("UserName", "Grace"));
    postData.Add(new KeyValuePair<string, string>("userID ", "12345"));
    var formContent = new FormUrlEncodedContent(postData);

    var response = client.PostAsync(url, formContent).Result;
    string resultContent = response.Content.ReadAsStringAsync().Result;
}


postData 값을 넘기는 방법은 여러가지가 있다.

http://stackoverflow.com/questions/3001108/namevaluecollection-vs-dictionarystring-string

에 Dictionary<string,string> 와 NameValueCollection 차이점에 대한 문의 내용인데 간략한 답변이 나온다.

블로그 이미지

Link2Me

,