Created
March 4, 2015 19:34
-
-
Save jltrem/815d59d89070a9b0e0c3 to your computer and use it in GitHub Desktop.
web request by client, sending cookies to server
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
private HttpWebRequest CreateWebRequest(ApiCommands cmd, List<string> restParams, bool post = false) | |
{ | |
var url = new StringBuilder(); | |
url.AppendFormat("http://localhost:8080/api/v1/{0}", cmd); | |
if (restParams != null) | |
{ | |
restParams.ForEach(x => url.AppendFormat("/{0}", x)); | |
} | |
var request = (HttpWebRequest)WebRequest.Create(url.ToString()); | |
request.ContentType = "application/json"; | |
request.Method = post ? "POST" : "GET"; | |
// include any cookies that we've received from the server | |
lock (_mutex) | |
{ | |
if (_cookies.Any()) | |
{ | |
request.CookieContainer = new CookieContainer(); | |
foreach (var x in _cookies) | |
{ | |
request.CookieContainer.Add(new Uri("http://localhost"), new Cookie(x.Name, x.Value)); | |
} | |
} | |
} | |
textBoxSendJson.Text = ""; | |
textBoxSendUrl.Text = string.Format("{0} {1}", request.Method, url); | |
return request; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment