在C#应用程序中打开此URL的正确方法
我想在我的C#应用程序中打开一个URL。此URL用于与通信设备通信,而不是与internet网站通信。我已经(我想)通过了所有的认证。但是我在程序中得到的文本与我使用web浏览器时正确显示的文本不同 这是密码在C#应用程序中打开此URL的正确方法,c#,https,C#,Https,我想在我的C#应用程序中打开一个URL。此URL用于与通信设备通信,而不是与internet网站通信。我已经(我想)通过了所有的认证。但是我在程序中得到的文本与我使用web浏览器时正确显示的文本不同 这是密码 using System; using System.IO; using System.Net; using System.Text; using System.Net.Security; using System.Security.Cryptography.X509Certificate
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;
}
}
}
结果虽然不能在Chrome中完美显示,但应该是:
NANA
NA
4c:cc:34:02:6d:26
00:23:A7:24:A3:B6
但我在控制台窗口中看到的文本是:
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>
您正在使用http方法POST,但您在评论中的url看起来更像GET,因此您可能需要
WebRequest.Create(url+数据)
不正确的响应是https://192.168.30.15/cgi-bin/
如果你将其放入Chrome浏览器中,会得到同样的“错误”响应
您可能不需要将任何数据写入请求流,并且可以更改请求的
方法和内容类型。解决方案需要两部分
首先,做正确的事情,这样就需要对代码进行彻底的返工
我担心“服务器违反了协议。Section=ResponseHeader Detail=Header name无效”。我试图为这项工作制作编程解决方案,但它是一个.NET2.0解决方案,我无法在.NET4+中找到它。因此,我编辑了.config文件并继续
以下是最终代码:
//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();
这已添加到调试文件夹中的.config文件中(并将使用VS2013添加到发布文件夹中)
谢谢所有回复的人。灵感帮助我找到了解决方案。您收到的是IIS的目录结构,您不应该使用以下URL执行get操作吗?您正在向一个显然是get请求的URL发出POST请求。只需将URL更改为https://192.168.30.15/cgi-bin/connstatus?202
,不向请求流写入任何内容,并将方法设置为GET
。完成。Rob,我做了你建议的更改,我得到了一个新的例外,正如我原始帖子底部的更新代码所指出的:“无法发送具有此动词类型的内容正文。”
//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();
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
</system.net>