我有这个功能来检索CookieContainer中的响应cookie(this.cookies)
private void getCookies(string url)
{
// request
HttpWebRequest request = CreateWebRequestObject(url);
request.CookieContainer = this.cookies;
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Headers.Add("Accept-Language", " es-MX,es;q=0.8,en-us;q=0.5,en;q=0.3");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2";
// response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
foreach (Cookie c in response.Cookies)
{
this.cookies.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在Fiddler中调试请求时,我得到了这个:

为什么饼干中有"$"?
根据MSDN
public Cookie(字符串名称,字符串值,字符串路径,字符串域)
name 类型:System.String Cookie的名称.不得在名称内使用以下字符:等号,分号,逗号,换行符(\n),返回(\ r),制表符(\ t)和空格字符.美元符号字符("$")不能是第一个字符.
如何删除此字符?
我需要使用HttpWebRequest发送这样的请求:
POST https://sap.site.com.mx/sap/bw/BEx?SAP-LANGUAGE=ES&PAGENO=1&CMD=PROCESS_VARIABLES&REQUEST_NO=0&CMD=PROCESS_VARIABLES&SUBCMD=VAR_SUBMIT&VARID= HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-MX,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
但是,我无法发送Connection标头.这是我的代码:
// request
HttpWebRequest request = CreateWebRequestObject(url);
request.CookieContainer = this.cc;
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2";
// headers
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Headers.Add("Accept-Language", " es-MX,es;q=0.8,en-us;q=0.5,en;q=0.3");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.KeepAlive = true; // it does not work as expected
request.ServicePoint.Expect100Continue = false; // remove Expect header
// post
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
using …Run Code Online (Sandbox Code Playgroud) 我需要创建一个具有两个属性的类:
这些属性(Actions <>)根据目标函数发送消息或异常.此目标函数通过属性设置.
目前,我有这个功能代码:
public class Output
{
private Action<string> logOutput;
private Action<Exception, string> exceptionOutput;
public Action<string> LogOutput { set { this.logOutput = value; } get { return this.logOutput; } }
public Action<Exception, string> ExceptionOutput { set { this.exceptionOutput = value; } get { return this.exceptionOutput; } }
public Output() : this(null, null) { }
public Output(Action<string> logAction, Action<Exception, string> exceptionAction)
{
this.logOutput = logAction;
this.exceptionOutput = exceptionAction;
}
public void WriteLogMessage(string format, params object[] args)
{
if …Run Code Online (Sandbox Code Playgroud)