Last active
August 29, 2015 13:57
-
-
Save lucasjans/9478677 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have an example in any other language, please include it!