使用POST从C#通过HTTPS发布 [英] Posting using POST from C# over https

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

问题描述

在浪费两天<一个href="http://stackoverflow.com/questions/768030/simple-httpwebrequest-over-ssl-https-gives-404-not-found-under-c">this问题(并试图使其工作),我决定退一步并问一个更基本的问题,因为显然有什么东西我不知道或我做错了。

的要求很简单,我需要做一个HTTP POST通过https 的从C#(通过几个值)。

网站(如果有合适的值),会返回一些简单的HTML和响应code。 (我会告诉这些后来)。

这是真正的简单。在Web服务的作品。我有一个PHP样本工程,并成功地连接到它。我也有一个Delphi的演示应用程序(附源$ C ​​$ C)也有效。最后我必须从具有服务的公司,这也适用过程的演示应用程序(二进制)。

不过,我需要通过C#来做到这一点。这听起来很简单,它是不工作。

有关测试目的,我创建了一个简单的控制台应用程序和一个简单的连接方法。我试着像7种不同的方法来创建一个HTTP请求,都或多或少同样的事情,不同的实现(使用Web客户端,使用HttpWebRequest的,等等)。

每一个方法的工作,除了当URI开头的https。

我得到一个webexception说,远程服务器返回404我已经安装了小提琴手(所建议的一个SO用户),并研究了一点点的流量。 404是因为我传递一些错误,因为我后来所提到的,服务的工作。我将讨论的小提琴手结果后。

在那里我把数据上传网址为:https://servicios.mensario.com/enviomasivo/apip/

这是POST数据:(该值是假货)

usuario=SomeUser&clave=SomePassword&nserie=01234567890123456789&version=01010000&operacion=220

服务器可能会返回两/三线响应(抱歉西班牙,但该公司是来自西班牙)。这里有一个可能的响应示例:

  HTTP / 1.1 200 OK
内容类型:纯文本/

01010000 100 BIEN
998
 

和这里的另一个

  HTTP / 1.1 200 OK
内容类型:纯文本/

01010000 20 AUTENTIFICACION NEGATIVA
哈habido联合国problema EN LAidentificación事前报servidor。 Corrija SUS DATOS德autentificacion。
 

第1手段之一确定,:第二个是验证失败。

正如你可以看到任务是相当的轻松的,只是它不工作。如果我使用的小提琴手,我看到有某种SSL的东西怎么回事的连接,然后一切工作正常。然而,就我读过,.NET处理所有的东西对我们(的是的,我已经添加了回调总是验证无效的证书的)。我不明白我在做什么错。我可以张贴/发送电子邮件至code,但我想知道的是非常简单的:

如何让一个POST通过SSL使用C#和简单HttpWebRequest和后来对处理字符串/数组/不管是什么反应?

相信我,当我说我一直在谷歌上搜索,并Stackoverflowing两天。我没有任何形式的代理权。通过我的路由器的连接通过。标准端口。没什么特别的。我的机器是VMware虚拟机中,是Windows Vista中,但考虑到样本应用程序(PHP,德尔福,二进制)没有问题,所有的工作,我不认为这是一个问题)。

在不同的样品(没有二进制),可这里如果有人想看看他们

我倒是AP preciate任何帮助。如果有人想尝试用一个真正的用户名,我有一个演示的用户,我可以通过你的用户名/密码进行测试。我只有的一个的演示的用户(一个他们给了我),这就是为什么我不把它粘贴在这里。我不想洪水与测试用户)

我用

解决方案

看我的回答您的其他问题试过(样本内)。我相信你的问题可能不是你的C#code。 Web服务URL accually返回404与其他一些工具,我用,但它返回表示如果离开过来自Web服务的URL结尾的斜线的反应,所以我建议您尝试了。

奇怪的是,它似乎并不重要,如果尾随网址有当没有做SSL。奇怪的东西与Web服务器,我想。

After wasting two days with this question (and trying to make it work), I've decided to take a step back and ask a more basic question, because apparently there's something I don't know or I'm doing wrong.

The requirements are simple, I need to make an HTTP post (passing a few values) over https from C#.

The website (if given the appropriate values) will return some simple html and a response code. (i'll show these later).

It's really that simple. The "webservice" works. I have a php sample that works and successfully connects to it. I also have a Dephi "demo" application (with source code) that also works. And finally I have the demo application (binary) from the company that has the "service", that also works of course.

But I need to do it through C#. That that sounds so simple, it is not working.

For testing purposes I've created a simple console app and a simple connect method. I've tried like 7 different ways to create an HTTP request, all more or less the same thing, different implementation (Using WebClient, using HttpWebRequest, etc).

Every method works, except when the URI begins with 'https'.

I get a webexception saying that the remote server returned 404. I've installed Fiddler (as suggested by a SO user), and investigated a little bit the traffic. The 404 is because I am passing something wrong, because as I mentioned later, the 'service' works. I'll talk about the fiddler results later.

The URL where I have to POST the data is: https://servicios.mensario.com/enviomasivo/apip/

And this is the POST data: (the values are fakes)

usuario=SomeUser&clave=SomePassword&nserie=01234567890123456789&version=01010000&operacion=220

The server might return a two/three lines response (sorry about the spanish, but the company is from Spain). Here's a sample of a possible response:

HTTP/1.1 200 OK
Content-Type: text/plain

01010000 100 BIEN
998

And here's another

HTTP/1.1 200 OK
Content-Type: text/plain

01010000 20 AUTENTIFICACION NEGATIVA
Ha habido un problema en la identificación ante el servidor. Corrija sus datos de autentificacion.

The 1st one means OK, and the 2nd one is Auth Failure.

As you can see the task is quite easy, only it doesn't work. If I use fiddler, I see that there's some sort of SSL stuff going on in the connection and then everything works fine. However, as far as I've read, .NET handles all that stuff for us (yes, i've added the callback to always validate invalid certs). I don't understand what I'm doing wrong. I can post/email the code, but what I'd like to know is very simple:

How can you make a POST over SSL using C# and a "simple" HttpWebRequest and later have the response in a string/array/Whatever for processing?

Trust me when I say I've been googling and Stackoverflowing for two days. I don't have any sort of proxy. The connection passes through my router. Standard ports. Nothing fancy. My Machine is inside a VMWare virtual machine and is Windows Vista, but given that the sample applications (php, delphi, binary) all work without an issue, I cannot see that as a problem).

The different samples (sans the binary) are available here if anyone wants to take a look at them.

I'd appreciate any help. If anyone wants to try with a "real" username, I have a demo user and I could pass you the user/pass for testing purposes. I only have one demo user (the one they gave me) and that's why I'm not pasting it here. I don't want to flood the user with tests ;)

I've tried (within the samples) using

解决方案

see my answer to your other question. I believe your problem may not be your C# code. The web service URL accually returns a 404 with several other tools I used, but it returns the response you indicated if you leave off the trailing slash from the web service URL, so I suggest trying that.

Oddly, it doesn't seem to matter if the trailing URL is there when not doing SSL. Something strange with that web server, I guess.

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

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