public override bool OnBeforeRequestSent(HttpRequestData data) { var header = new HttpRequestHeader { Name = "x-request-time", Value = DateTime.UtcNow.ToString("o") }; data.Headers.Add(header); if (!string.IsNullOrEmpty(data.RequestContent)) { // do something here to get your unique value var userId = new Random().Next().ToString(); // then embed it into the content data.RequestContent = data.RequestContent.Replace("##UserId##", "USER_" + userId); } return true; }
/// <summary> /// Parses Request Http Headers from an array of strings /// into the Headers property of this class. /// </summary> /// <param name="lines">Array of Http Headers to parse in header: value format</param> public void ParseHttpHeaders(string[] lines) { Headers.Clear(); for (int i = 0; i < lines.Length; i++) { string line = lines[i]; if (string.IsNullOrEmpty(line)) continue; int idx = line.IndexOf(':'); if (idx < 0 || line.Length < idx + 2) continue; string header = line.Substring(0, idx); string value = line.Substring(idx + 1).Trim(); var hd = new HttpRequestHeader { Name = header, Value = value }; var name = hd.Name.ToLower(); // ignore host header - host is part of url if (name == "host") { Host = hd.Value; continue; } if (name == "content-type") { ContentType = hd.Value; if (!string.IsNullOrEmpty(hd.Value)) { TextEncoding = StringUtils.ExtractString(hd.Value, "charset=", ";", false, true); // Web Content defaults to UTF-8 if (string.IsNullOrEmpty(TextEncoding)) TextEncoding = "UTF-8"; } } if (name == "content-length") continue; // HTTP client adds this automatically if (name == "websurge-request-inactive") { IsActive = false; continue; // don't add header } if (name == "websurge-request-name") { Name = hd.Value; continue; } Headers.Add(hd); } }
/// <summary> /// Parses Request Http Headers from an array of strings /// into the Headers property of this class. /// </summary> /// <param name="lines">Array of Http Headers to parse in header: value format</param> public void ParseHttpHeaders(string[] lines) { Headers.Clear(); for (int i = 0; i < lines.Length; i++) { string line = lines[i]; if (string.IsNullOrEmpty(line)) { continue; } int idx = line.IndexOf(':'); if (idx < 0 || line.Length < idx + 2) { continue; } string header = line.Substring(0, idx); string value = line.Substring(idx + 1).Trim(); var hd = new HttpRequestHeader { Name = header, Value = value }; var name = hd.Name.ToLower(); // ignore host header - host is part of url if (name == "host") { Host = hd.Value; continue; } if (name == "content-type") { ContentType = hd.Value; if (!string.IsNullOrEmpty(hd.Value)) { TextEncoding = StringUtils.ExtractString(hd.Value, "charset=", ";", false, true); // Web Content defaults to UTF-8 if (string.IsNullOrEmpty(TextEncoding)) { TextEncoding = "UTF-8"; } } } if (name == "content-length") { continue; // HTTP client adds this automatically } if (name == "websurge-request-inactive") { IsActive = false; continue; // don't add header } if (name == "websurge-request-name") { Name = hd.Value; continue; } Headers.Add(hd); } }