西里尔文 POST 标头的值
这是我的简单 C# 代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace Test
{
class Program
{
static void Main(string[] args)
{
WebRequest req = WebRequest.Create("http://192.168.1.35:8888/");
req.Method = "POST";
req.ContentLength = 0;
req.Headers.Add("s", "АБВ12");
req.Headers.Add("username", "user");
req.Headers.Add("password", "pass");
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
Console.WriteLine(sr.ReadToEnd());
}
}
}
因此,我尝试向 Apache 服务器发送 POST 请求并获取服务器答案。我不需要任何额外的请求标头。问题是,然后我尝试运行此代码,但出现异常:
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
UPD 我通过使用互操作组件 WinHttpRequest 解决了这个问题:
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();
args[0] 包含任何西里尔字符。谢谢大家!
Here is my simple C# code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace Test
{
class Program
{
static void Main(string[] args)
{
WebRequest req = WebRequest.Create("http://192.168.1.35:8888/");
req.Method = "POST";
req.ContentLength = 0;
req.Headers.Add("s", "АБВ12");
req.Headers.Add("username", "user");
req.Headers.Add("password", "pass");
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
Console.WriteLine(sr.ReadToEnd());
}
}
}
So, I trying to send POST request to Apache Server and get server answer. I don't need any additional request heeaders. The problem is then I tried to run this code I got exception:
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:
It seems like I need to convert header value to ISO-8859-1 encoding. So, how can I can get this program working properly? Sorry for my english. I hope for your help.
Thanks in advance!
Sample request that works correctly in my situation:
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
UPD
I've solve this problem by using Interop component WinHttpRequest:
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();
args[0] contains any cyrillic charachters. Thanks everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Uri.EscapeDataString
对请求标头中的非 ASCII 字符进行转义。下面的代码(使用简单的 WCF 服务来模拟接收方)显示了如何完成此操作。请注意,您还需要在服务器端对标头值进行转义(也如下所示)。You can use
Uri.EscapeDataString
to escape the non-ASCII character in the request header. The code below (with a simple WCF service to simulate the receiving side) shows how this can be done. Notice that you'll also need to unescape the header value at the server side (shown below as well).