从 Php 到 C# 的 https POST
我必须向第三方 https url 发布帖子才能处理并发回数据。我所举的例子是这样的:
$signature= foo_string;
$data_to_post = json_dictionary;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("JSON-Signature: $signature"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_to_post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
当我们使用 ASP .NET C# 2.0 时,我必须移植它,但我总是收到未验证的错误。
这就是我正在做的事情:
HttpWebRequest q = (HttpWebRequest)WebRequest.Create(Host + ":" + Port);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(new interhanse().AcceptAllCertifications);
q.Method = "POST";
q.Headers.Add("JSON-Signature:" + GetSignature(data));
q.ContentType = "application/json";
q.UseDefaultCredentials = false;
q.Credentials = new NetworkCredential(user,pwd, Host);
byte[] buffer = UTF8Encoding.UTF8.GetBytes(data);
q.ContentLength = data.Length;
Stream oStream = q.GetRequestStream();
StreamWriter oWriter = new StreamWriter(oStream);
oWriter.Write(buffer);
oWriter.Close();
HttpWebResponse reps = q.GetResponse() as HttpWebResponse;
我已经阅读了我能找到的所有关于此问题的问题,但我没有得到任何改进。提前致谢!
I have to make a post to a third party https url to get data procesed and sent back. And all I have as an example is this:
$signature= foo_string;
$data_to_post = json_dictionary;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("JSON-Signature: $signature"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_to_post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
As we work with ASP .NET C# 2.0, I have to port this, but I get always a not autenticated error.
Here is what I'm doing:
HttpWebRequest q = (HttpWebRequest)WebRequest.Create(Host + ":" + Port);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(new interhanse().AcceptAllCertifications);
q.Method = "POST";
q.Headers.Add("JSON-Signature:" + GetSignature(data));
q.ContentType = "application/json";
q.UseDefaultCredentials = false;
q.Credentials = new NetworkCredential(user,pwd, Host);
byte[] buffer = UTF8Encoding.UTF8.GetBytes(data);
q.ContentLength = data.Length;
Stream oStream = q.GetRequestStream();
StreamWriter oWriter = new StreamWriter(oStream);
oWriter.Write(buffer);
oWriter.Close();
HttpWebResponse reps = q.GetResponse() as HttpWebResponse;
I've read all SO questions I can find about this, but I don't get any improvements. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您做错的一件事是假设以字节为单位的长度与以字符为单位的长度相同。您应该使用 buffer.Length 作为内容长度。您还使用字节数组调用
StreamWriter.Write
。您不应该这样做 - 您应该只使用流,因为您已经完成了编码:现在,这并不能解决身份验证问题。您可能会发现只需设置
PreAuthenticate
即可解决该问题:如果这不起作用,我建议您运行 WireShark 并查看通过 Curl 发出的请求与来自 .NET 的请求之间的差异。
Well, one thing you're doing wrong is assuming that the length in bytes is the same as the length in characters. You should use buffer.Length for the content length. You're also calling
StreamWriter.Write
with a byte array. You shouldn't do that - you should just use the stream, as you've already done the encoding:Now, that won't solve the authentication issue. You may find that just setting
PreAuthenticate
solves that though:If that doesn't work, I suggest you run WireShark and look at the differences between the request through Curl and the request from .NET.
我认为您不应该在身份验证中提供主机...
这将类似于:
您还应该避免在每个请求上分配服务点验证委托,这可能会逐渐减慢请求,因为验证会执行多次,并且这也是一种内存泄漏。
I think you should not supply the host in the authentication...
Which would be something like:
Also you should avoid assigning the service point validation delegate on each request, this may slow down the requests increasingly because the validation is performed multiple times, and it's also kind of a memory leak.
以下是在 Asp.Net 中添加 CURLOPT_USERPWD 的方法:
Here is how you add that CURLOPT_USERPWD in Asp.Net: