Skip to content

Instantly share code, notes, and snippets.

@jltrem
Created March 4, 2015 19:34
Show Gist options
  • Save jltrem/815d59d89070a9b0e0c3 to your computer and use it in GitHub Desktop.
Save jltrem/815d59d89070a9b0e0c3 to your computer and use it in GitHub Desktop.
web request by client, sending cookies to server
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