<commit_before>﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using KNKVPlugin.DataTypes;
using Newtonsoft.Json;

namespace KNKVPlugin
{
	public class Request
	{
		private readonly WebRequest _request;

		public Request(string code)
		{
			var serviceUrl = String.Format("http://www.knkv.nl/kcp/{0}/json/", code);
			_request = WebRequest.Create(serviceUrl);
			_request.ContentType = "application/x-www-form-urlencoded";
			_request.Method = WebRequestMethods.Http.Post;
		}


		public Teams GetTeams()
		{
			var queryString = HttpUtility.ParseQueryString(String.Empty);
			queryString["file"] = "json";
			queryString["f"] = "get_data";
			queryString["t"] = "teams";
			queryString["t_id"] = "";
			queryString["p"] = "0";
			queryString["full"] = "0";
			return JsonConvert.DeserializeObject<Teams>(Execute(queryString));
		}


		private string Execute(NameValueCollection postCollection)
		{
			var postData = postCollection.ToString();
			var byteArray = new ASCIIEncoding().GetBytes(postData);
			_request.ContentLength = byteArray.Length;

			Stream stream = null;
			StreamReader streamReader = null;
			WebResponse response = null;

			try
			{
				// POST
				stream = _request.GetRequestStream();
				stream.Write(byteArray, 0, byteArray.Length);
				stream.Close();

				// GET RESPONSE
				response = _request.GetResponse();
				stream = response.GetResponseStream();

				// READ RESPONSE
				streamReader = new StreamReader(stream);
				var responseFromServer = streamReader.ReadToEnd();<commit_msg>Handle non-valid JSON<commit_after>﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using KNKVPlugin.DataTypes;
using Newtonsoft.Json;

namespace KNKVPlugin
{
	public class Request
	{
		private readonly WebRequest _request;

		public Request(string code)
		{
			var serviceUrl = String.Format("http://www.knkv.nl/kcp/{0}/json/", code);
			_request = WebRequest.Create(serviceUrl);
			_request.ContentType = "application/x-www-form-urlencoded";
			_request.Method = WebRequestMethods.Http.Post;
		}


		public Teams GetTeams()
		{
			var queryString = HttpUtility.ParseQueryString(String.Empty);
			queryString["file"] = "json";
			queryString["f"] = "get_data";
			queryString["t"] = "teams";
			queryString["t_id"] = "";
			queryString["p"] = "0";
			queryString["full"] = "0";

			var response = Execute(queryString);

			try
			{
				var jObject = JsonConvert.DeserializeObject<Teams>(response);
				return jObject;
			}
			catch (JsonReaderException e)
			{
				// No valid JSON was recieved. Throw the ugly html error that the service is returning.
				throw new ApplicationException(response);
			}
			
		}


		private string Execute(NameValueCollection postCollection)
		{
			var postData = postCollection.ToString();
			var byteArray = new ASCIIEncoding().GetBytes(postData);
			_request.ContentLength = byteArray.Length;

			Stream stream = null;
			StreamReader streamReader = null;
			WebResponse response = null;

			try
			{
				// POST
				stream = _request.GetRequestStream();
				stream.Write(byteArray, 0, byteArray.Length);
				stream.Close();

				// GET RESPONSE
				response = _request.GetResponse();
				stream = response.GetResponseStream();

				// READ RESPONSE
				streamReader = new StreamReader(stream);
				var responseFromServer = streamReader.ReadToEnd();
{'fraction_non_alphanumeric': 0.11979166666666667, 'fraction_numerical': 0.0023148148148148147, 'mean_word_length': 3.9174964438122335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2931884', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1045, 'n_words': 231}