Https POST从Php到C# [英] Https POST from Php to C#

查看:136
本文介绍了Https POST从Php到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须向第三方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;

我已经阅读了所有关于此问题的SO问题,但我没有得到任何改进。在此先感谢!

解决方案

嗯,你做错了一件事是假设字节的长度字符中的长度相同。您应该使用buffer.Length作为内容长度。您还使用字节数组调用 StreamWriter.Write 。你不应该这样做 - 你应该只使用流,因为你已经完成了编码:

  byte [] buffer = Encoding.UTF8.GetBytes(data); 

q.ContentLength = buffer.Length;
using(Stream stream = q.GetRequestStream())
{
stream.Write(buffer,0,buffer.Length);
}

现在,这不会解决身份验证问题。您可能会发现只设置 PreAuthenticate 解决了这个问题:

  q。 PreAuthenticate = true; 

如果这不起作用,我建议你运行 WireShark 并查看Curl请求与.NET请求之间的差异。


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!

解决方案

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:

byte[] buffer = Encoding.UTF8.GetBytes(data);

q.ContentLength = buffer.Length;
using (Stream stream = q.GetRequestStream())
{
    stream.Write(buffer, 0, buffer.Length);
}

Now, that won't solve the authentication issue. You may find that just setting PreAuthenticate solves that though:

q.PreAuthenticate = true;

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.

这篇关于Https POST从Php到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆