Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created November 11, 2015 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TinkerWorX/dfa98340762cad308674 to your computer and use it in GitHub Desktop.
Save TinkerWorX/dfa98340762cad308674 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinkerWorX.SharpCraft;
using MindWorX.SharpCraft.Modules.JassAPI;
using System.Net;
using System.IO;
namespace Http
{
[Requires(typeof(JassAPIPlugin))]
public class HttpPlugin : IPlugin
{
public void Initialize(PluginContext context)
{
// the context is used to make game/editor specific initializing
// for this plugin all we care about is the game
if (context != PluginContext.Game)
return;
Natives.Add(new HttpRequestPrototype(this.HttpRequest));
}
public void OnGameLoad(PluginContext context)
{
// the context is used to make game/editor specific initializing
// for this plugin all we care about is the game
if (context != PluginContext.Game)
return;
}
private delegate JassStringArg HttpRequestPrototype(JassStringArg method, JassStringArg url, JassStringArg cookies, JassStringArg values);
private JassStringArg HttpRequest(JassStringArg method, JassStringArg url, JassStringArg cookies, JassStringArg values)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
CookieContainer container = new CookieContainer();
req.Method = method;
req.UserAgent = "Warcraft III";
req.ContentType = "application/x-www-form-urlencoded";
Uri uri = new Uri(url);
if (cookies != "")
{
string[] cookArr = cookies.ToString().Split(new string[] { ";" }, StringSplitOptions.None);
string[] temp;
foreach (string cook in cookArr)
{
if (cook.Contains("="))
{
temp = cook.Split(new string[] { "=" }, StringSplitOptions.None);
container.Add(uri, new Cookie(temp[0], temp[1]));
}
}
}
req.CookieContainer = container;
System.Net.ServicePointManager.Expect100Continue = false;
// prevents 417 error
if (values != "")
using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
{
writer.Write(values);
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream());
string output = reader.ReadToEnd();
reader.Close();
return output;
}
catch(Exception e)
{
// this is bad, but at least we won't crash.
Natives.DisplayTimedTextToPlayer(JassPlayer.FromLocal(), 0.00f, 0.00f, 60.00f, e.ToString());
}
return String.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment