by
3
7
3,709
6
Top 1% !
Famous
Tagged
Easy-to-find
Refined
Tuned
OpenSource
Popularity: 3036th place
.NETFramework4.5
Architecturex86_64
ClassNameAJAX
Extension.cs
InputName0URL
InputType0string
LanguageCSharp
MethodNameGET
OutputTypestring
TechnologyAJAX

AJAX port for C#

This is not a genuine Ajax Request but have the same behavior. Simply call the AJAX.GET, AJAX.POST or AJAX.JSON methods.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/793438/AJAX-port-for-C-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public class AJAX { public static string GET(string URL) { string data = ""; if (!string.IsNullOrWhiteSpace(URL)) { try { WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; data = client.DownloadString(URL); client.Dispose(); } catch (Exception ex) { data = ex.Message; } } return data; } public byte[] GETFile(string URL) { using (WebClient Client = new WebClient()) { return Client.DownloadData(URL); } } public object POST(string URL, string PostData) { WebRequest request = WebRequest.Create(""); request.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(PostData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } public void POSTFile(string URL, FileInfo TheFile) { using (System.Net.WebClient wc = new System.Net.WebClient()) { wc.UploadFile(URL, TheFile.FullName); } } public static dynamic JSON(string URL) { string json = GET(URL); dynamic result = new JavaScriptSerializer().Deserialize<dynamic>(json); return result; } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus