Microsoft.XMLHTTP 代码转换为 C# 时出现内部服务器错误
Internal Server Error with Microsoft.XMLHTTP code converted to C#
我继承了一些将一些数据上传到网站的 VB6 代码。我正在尝试将其转换为 C#。我最初尝试使用 WebRequest
对象,但做了更多研究后,我尝试了 WebClient
。两个好像都有问题
这是我继承的代码:
' The object that will make the call to the WS
Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
' Tell the name of the subroutine that will handle the response
'oXMLHTTP.onreadystatechange = HandleStateChange
' Initializes the request (the last parameter, False in this case, tells if the call is asynchronous or not
oXMLHTTP.Open "POST", "https://path.to.webpage/Update.asmx/UpdatePage", False
' This is the content type that is expected by the WS using the HTTP POST protocol
oXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'Now we send the request to the WS
oXMLHTTP.send "userName=user&password=password&html=" & ThisMessage
ThisMessage
其实是动态创建了一个字符串html.
这是 vb6 代码的 c# 翻译:
public static void PostHTML(string uri)
{
NetworkCredential credential = new NetworkCredential("user", "password");
WebClient request = new WebClient();
request.UseDefaultCredentials = false;
request.Credentials = credential;
request.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string postString = GetWebTemplate();
//byte[] byteArray = Encoding.UTF8.GetBytes(postData);
var response = request.UploadString(uri,"POST", postString);
Debug.WriteLine(response);
request.Dispose();
}
这是纯 "test" 代码。 uri 是 "https://path.to.webpage/Update.asmx/UpdatePage"
,虽然 postString
与 thisMessage
不同,但它是一个有效的 html 页面。
我试过 request.UploadString()
和 request.UploadData()
(使用已被注释掉的 byteArray)。我也尝试过更改编码。
我遇到的问题是:
Exception thrown: 'System.Net.WebException' in System.dll
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (500) Internal Server Error.
我不确定为什么会收到内部服务器错误,因为 VB6 代码仍然很高兴 运行 没有错误!
有什么建议吗?
根据@pcdev 的建议,这是最终有效的代码:
public static void PostHTML(string uri)
{
WebClient request = new WebClient();
request.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string postString = "userName=user&password=password&html=" + GetWebTemplate();
var response = request.UploadString(uri,"POST", postString);
Debug.WriteLine(response);
request.Dispose();
}
我继承了一些将一些数据上传到网站的 VB6 代码。我正在尝试将其转换为 C#。我最初尝试使用 WebRequest
对象,但做了更多研究后,我尝试了 WebClient
。两个好像都有问题
这是我继承的代码:
' The object that will make the call to the WS
Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
' Tell the name of the subroutine that will handle the response
'oXMLHTTP.onreadystatechange = HandleStateChange
' Initializes the request (the last parameter, False in this case, tells if the call is asynchronous or not
oXMLHTTP.Open "POST", "https://path.to.webpage/Update.asmx/UpdatePage", False
' This is the content type that is expected by the WS using the HTTP POST protocol
oXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'Now we send the request to the WS
oXMLHTTP.send "userName=user&password=password&html=" & ThisMessage
ThisMessage
其实是动态创建了一个字符串html.
这是 vb6 代码的 c# 翻译:
public static void PostHTML(string uri)
{
NetworkCredential credential = new NetworkCredential("user", "password");
WebClient request = new WebClient();
request.UseDefaultCredentials = false;
request.Credentials = credential;
request.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string postString = GetWebTemplate();
//byte[] byteArray = Encoding.UTF8.GetBytes(postData);
var response = request.UploadString(uri,"POST", postString);
Debug.WriteLine(response);
request.Dispose();
}
这是纯 "test" 代码。 uri 是 "https://path.to.webpage/Update.asmx/UpdatePage"
,虽然 postString
与 thisMessage
不同,但它是一个有效的 html 页面。
我试过 request.UploadString()
和 request.UploadData()
(使用已被注释掉的 byteArray)。我也尝试过更改编码。
我遇到的问题是:
Exception thrown: 'System.Net.WebException' in System.dll An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The remote server returned an error: (500) Internal Server Error.
我不确定为什么会收到内部服务器错误,因为 VB6 代码仍然很高兴 运行 没有错误!
有什么建议吗?
根据@pcdev 的建议,这是最终有效的代码:
public static void PostHTML(string uri)
{
WebClient request = new WebClient();
request.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string postString = "userName=user&password=password&html=" + GetWebTemplate();
var response = request.UploadString(uri,"POST", postString);
Debug.WriteLine(response);
request.Dispose();
}