Skip to content

Instantly share code, notes, and snippets.

@nicklasos
Last active August 29, 2015 14:01
Show Gist options
  • Save nicklasos/7f68f45129d8a981087f to your computer and use it in GitHub Desktop.
Save nicklasos/7f68f45129d8a981087f to your computer and use it in GitHub Desktop.
C# Common functions
public struct MainPage
{
public string Threads { get; set; }
public List<Pager> Pages { get; set; }
}
public struct Pager
{
private string filename;
public string Filename
{
get
{
return Page == 0 ? filename.Replace("0", "wakaba") : filename;
}
set
{
filename = value;
}
}
public int Page { get; set; }
}
string json = @"
{
'threads': 'bla',
'pages': [
{
'filename': '/b/0.json',
'page': 0
},
{
'filename': '/b/1.json',
'page': 1
},
{
'filename': '/b/2.json',
'page': 2
},
{
'foo': 'baaaar'
}
]
}
";
MainPage mainPage = JsonConvert.DeserializeObject<MainPage>(json);
Console.WriteLine(mainPage.Threads);
foreach (Pager currentPage in mainPage.Pages)
{
Console.WriteLine(currentPage.Filename);
}
string text = string.Empty;
using (StreamReader streamReader = new StreamReader("wakaba.json", Encoding.UTF8))
{
text = streamReader.ReadToEnd();
}
using (WebClient webClient = new WebClient())
{
webClient.QueryString.Add("param1", "value1");
webClient.QueryString.Add("param2", "value2");
string result = webClient.DownloadString("http://theurl.com");
}
string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1&param2=value2&param3=value3";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
}
string sURL = "http://www.microsoft.com";
WebRequest wrGETURL = WebRequest.Create(sURL);
WebResponse objResponse = wrGETURL.GetResponse();
Stream objStream = objResponse.GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string response = objReader.ReadToEnd();
objStream.Close();
objReader.Close();
objResponse.Close();
Console.WriteLine(response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment