Skip to content

Instantly share code, notes, and snippets.

@lucasjans
Last active August 29, 2015 13:57
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 lucasjans/9478677 to your computer and use it in GitHub Desktop.
Save lucasjans/9478677 to your computer and use it in GitHub Desktop.
//using system
//using system.net
private static string WebRequestHelper(string url, string jsonPostData, string method, string email, string password)
{
try
{
var uri = new Uri(url);
var request = HttpWebRequest.Create(uri);
// This sets up the basic authentication credentials
request.Credentials = new NetworkCredential(email, password);
request.Method = method;
// This could be application/xml, if you're working with XML data.
request.ContentType = "application/json";
if (!string.IsNullOrEmpty(jsonPostData))
{
using (var s = request.GetRequestStream())
{
using (var sw = new System.IO.StreamWriter(s))
sw.Write(jsonPostData);
}
}
using (var s = request.GetResponse().GetResponseStream())
{
using (var sr = new System.IO.StreamReader(s))
{
var jsonResponse = sr.ReadToEnd();
return jsonResponse;
}
}
}
catch (Exception er)
{
throw;
}
}
// requires jquery
var WebRequestHelper = new function(url, jsonPostData, method, email, password, successCallback, failureCallback) {
$.ajax
({
type: method,
url: url,
dataType: 'json',
username: username,
password: password,
data: jsonPostData,
success: successCallback,
failure: failureCallback
}
});
}
@lucasjans
Copy link
Author

If you have an example in any other language, please include it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment