Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 西里尔文柱头';s值_C#_Post_Http Headers_Webrequest - Fatal编程技术网

C# 西里尔文柱头';s值

C# 西里尔文柱头';s值,c#,post,http-headers,webrequest,C#,Post,Http Headers,Webrequest,下面是我的简单C代码: 所以,我试图将POST请求发送到Apache服务器并获得服务器的响应。我不需要任何额外的请求。问题是,我尝试运行此代码时出现异常: System.ArgumentException was unhandled Message=Specified value has invalid Control characters. Parameter name: value Source=System ParamName=value StackTrace:

下面是我的简单C代码:

所以,我试图将POST请求发送到Apache服务器并获得服务器的响应。我不需要任何额外的请求。问题是,我尝试运行此代码时出现异常:

System.ArgumentException was unhandled
  Message=Specified value has invalid Control characters.
Parameter name: value
  Source=System
  ParamName=value
  StackTrace:
       at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue)
       at System.Net.WebHeaderCollection.Add(String name, String value)
       at Test.Program.Main(String[] args) in D:\Test\Test\Test\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
似乎我需要将标题值转换为ISO-8859-1编码。那么,我怎样才能让这个程序正常工作呢?对不起我的英语。我希望得到你的帮助。 提前谢谢


在我的情况下正确工作的示例请求:

POST / HTTP/1.1 s: АБВ12 username: user password: pass Content-Length: 0 Accept: */* User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5) Host: 127.0.0.1 Connection: Keep-Alive
args[0]包含任何西里尔字符。谢谢大家

您可以使用
Uri.EscapeDataString
对请求头中的非ASCII字符进行转义。下面的代码(使用一个简单的WCF服务来模拟接收方)展示了如何做到这一点。注意,您还需要在服务器端取消扫描头值(如下所示)


谢谢你的回复,卡洛斯!抱歉,但我并没有写我不能控制服务器端(例如,我写了本地地址)。问题是您的代码不起作用,但这是一个很好的尝试。请求已发送到服务器,但未被“识别”,因为它试图在数据库中查找“s”头值的值。我在VisualFoxPro中有一个程序可以正确地与该web服务器一起工作(请参阅问题更新中的适当请求示例)。所以它似乎使用特定的crarterset将其发送到服务器。 POST / HTTP/1.1 s: АБВ12 username: user password: pass Content-Length: 0 Accept: */* User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5) Host: 127.0.0.1 Connection: Keep-Alive
WinHttp.WinHttpRequest oHTTP = new WinHttp.WinHttpRequest();
oHTTP.Open("POST", "http://127.0.0.1:8888/");
oHTTP.SetRequestHeader("s", args[0]);
oHTTP.SetRequestHeader("username", "user");
oHTTP.SetRequestHeader("password", "pass");
oHTTP.Send();
public class StackOverflow_6449723
{
    [ServiceContract]
    public class Service
    {
        [WebGet(UriTemplate = "*", ResponseFormat = WebMessageFormat.Json)]
        public Stream GetHeaders()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var header in WebOperationContext.Current.IncomingRequest.Headers.AllKeys)
            {
                sb.AppendLine(string.Format("{0}: {1}", header, Uri.UnescapeDataString(WebOperationContext.Current.IncomingRequest.Headers[header])));
            }
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain; charset=utf-8";
            return new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebRequest req = WebRequest.Create(baseAddress + "/foo");
        req.Headers.Add("s", Uri.EscapeDataString("АБВ12"));             
        req.Headers.Add("username", "user");
        req.Headers.Add("password", "pass");
        WebResponse resp = req.GetResponse();
        StreamReader sr = new StreamReader(resp.GetResponseStream());
        Console.WriteLine(sr.ReadToEnd()); 

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}