Skip to content

Instantly share code, notes, and snippets.

@robertkraig
Forked from dkrusky/Http Form Post
Last active February 25, 2017 09:18
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 robertkraig/751ca796fe7bc317d2bdb2ca5b9d7017 to your computer and use it in GitHub Desktop.
Save robertkraig/751ca796fe7bc317d2bdb2ca5b9d7017 to your computer and use it in GitHub Desktop.
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Specialized;
class DataService {
public NameValueCollection Query { get; set; }
public string Endpoint { get; set; }
public FormUrlEncodedContent QueryEncoded {
get {
try {
return new FormUrlEncodedContent( Query.AllKeys
.Select( s => new { Key = s, Value = Query[s] } )
.ToDictionary( p => p.Key, p => p.Value ) );
} catch {
return null;
}
}
}
public DataService() {
Query = new NameValueCollection();
}
public void ClearQuery() {
Query.Clear();
}
public async Task<string> Post() {
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string result = "";
using( HttpClient client = new HttpClient() ) {
HttpResponseMessage reply = await client.PostAsync( Endpoint, QueryEncoded );
result = await reply.Content.ReadAsStringAsync();
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment