[英]Proper Way to open this URL in a C# app
I have a URL that I want to open in my C# app. 我有一个要在C#应用程序中打开的URL。 This URL is used to talk to a communications device, not an internet web site.
此URL用于与通信设备(而不是Internet网站)进行通信。 I have gotten by (I think) all the cert stuff.
我已经了解了所有证书内容。 But the text I get back in the program IS NOT the same thing that CORRECTLY displays when I use a web browser.
但是我在程序中返回的文本与使用Web浏览器时正确显示的内容不同。
Here's the code. 这是代码。
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;
namespace VMLConnStatus
{
class Program
{
static void Main(string[] args)
{
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
// Create a request for the URL: https://192.168.30.15/cgi-bin/connstatus?202
String url = "https://192.168.30.15/cgi-bin/";
String data = "connstatus?202";
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = data;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
Console.ReadLine();
}
}
public class MyPolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint,
X509Certificate certificate, WebRequest request,
int certificateProblem)
{
//Return True to force the certificate to be accepted.
return true;
}
}
}
The result, though not perfectly displayed in Chrome, should be: 结果虽然不能在Chrome中完美显示,但应为:
NA 不适用
NA 不适用
NA 不适用
NA 不适用
4c:cc:34:02:6d:26 4c:cc:34:02:6d:26
00:23:A7:24:A3:B6 00:23:A7:24:A3:B6
But the text I get in the console window is: 但是我在控制台窗口中看到的文本是:
Ok
<HTML>
<HEAD><TITLE>Index of cgi-bin/</TITLE></HEAD>
<BODY BGCOLOR="#99cc99" TEXT="#000000" LINK="#2020ff" VLINK="#4040cc">
<H4>Index of cgi-bin/</H4>
<PRE>
<A HREF=".">. </A> 15Jun2014 09:48
0
<A HREF="..">.. </A> 15Jun2014 09:48
0
<A HREF="connstatus">connstatus </A> 15Jun2014 09:48
19580
<A HREF="firmwarecfg">firmwarecfg </A> 15Jun2014 09:48
45736
<A HREF="webcm">webcm </A> 15Jun2014 09:48
23836
</PRE>
<HR>
<ADDRESS><A HREF="http://www.acme.com/software/mini_httpd/">mini_httpd/1.19 19de
c2003</A></ADDRESS>
</BODY>
</HTML>
Not EVEN close to the same thing. 甚至没有接近同一件事。
What am I doing wrong? 我究竟做错了什么?
Chuck 卡盘
UPDATE: Code changed. 更新:代码已更改。 URL, GET, and request writing (presuming I understood the directions).
URL,GET和请求编写(假设我理解了说明)。 New code is:
新代码是:
static void Main(string[] args)
{
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
// Create a request for the URL: https://192.168.30.15/cgi-bin/connstatus?202
String url = "https://192.168.30.15/cgi-bin/connstatus?202";
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "GET";
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Get the request stream.
//Now it throws an exception here--------------------------------
//"Cannot send a content-body with this verb-type."
Stream dataStream = request.GetRequestStream();
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
Console.ReadLine();
}
You are using http method POST but the url you have in the comment looks more like GET so then you probably need WebRequest.Create(url + data)
. 您正在使用http方法POST,但是注释中的url更像是GET,因此您可能需要
WebRequest.Create(url + data)
。
The incorrect response is the index page for https://192.168.30.15/cgi-bin/
which if you put into Chrome will give you the same "wrong" response. 错误的响应是
https://192.168.30.15/cgi-bin/
的索引页,如果您将其插入Chrome,则会得到相同的“错误”响应。
You might not need to write any data to the request stream and can change the Method
and ContentType
for the request. 您可能不需要将任何数据写入请求流,并且可以更改请求的
Method
和ContentType
。
The solution required two parts. 解决方案需要两部分。
First, doing the proper things, thus a total code rework. 首先,做适当的事情,从而使整个代码重做。
I had the dreaded "The server committed a protocol violation. Section=ResponseHeader Detail=Header name is invalid". 我很害怕“服务器犯了协议冲突。Section = ResponseHeader Detail = Header名称无效”。 I tried to make the programatic solution for this work, but it is a .NET 2.0 solution and I was not able to figure it out in .NET4+.
我试图为该工作提供程序化的解决方案,但这是一个.NET 2.0解决方案,我无法在.NET4 +中弄清楚。 So, I edited the .config file and went on.
因此,我编辑了.config文件并继续。
Here's the final code: 这是最终代码:
//Initialization
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(@"https://192.168.30.15/cgi-bin/connstatus?202");
//method is GET.
WebReq.Method = "GET";
//Get the response handle
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//read the response
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
//display it
Console.WriteLine(_Answer.ReadToEnd());
//pause for the ENTER key
Console.ReadLine();
This was added to the .config file in the debug folder (and would be added in the Release folder also..... using VS2013) 这已添加到debug文件夹中的.config文件中(也将使用VS2013添加到Release文件夹中.....)
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
</system.net>
Thank-you to everyone that replied. 谢谢大家的答复。 The inspiration helped me get to the solution.
灵感帮助我找到了解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.