在c#应用程序中打开此URL的正确方法
本文关键字:URL 方法 应用程序 | 更新日期: 2023-09-27 18:08:44
我有一个URL,我想在我的c#应用程序中打开。这个URL用于与通信设备交谈,而不是互联网网站。我想我已经通过了所有的认证。但是,当我使用web浏览器时,我在程序中返回的文本与正确显示的文本不同。
代码如下:
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中,应该是:
NA NA
NA NA
4 c: 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>
根本不是一回事。
我做错了什么?
查克
UPDATE:代码已更改。URL、GET和请求写入(假设我理解了说明)。新代码为:
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();
}
您正在使用http方法POST,但您在评论中的url看起来更像GET,因此您可能需要WebRequest.Create(url + data)
。
错误的响应是https://192.168.30.15/cgi-bin/
的索引页,如果你把它放到Chrome浏览器中,会给你同样的"错误"响应。
您可能不需要向请求流写入任何数据,并且可以更改请求的Method
和ContentType
。
解决方案需要两个部分。
首先,做正确的事情,从而重新编写代码。
我看到了可怕的"服务器违反了协议。"Section=ResponseHeader Detail=报头名称无效"。我试图为这项工作做出编程解决方案,但它是一个。net 2.0解决方案,我无法在。net 4+中找到它。所以,我编辑了。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();
这被添加到debug文件夹中的.config文件中(也会被添加到Release文件夹.....中)使用VS2013)
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
</system.net>
感谢所有回复的人。灵感帮助我找到了解决方案。