繁体   English   中英

在ASP.NET中,这种经典的ASP代码是什么样的?

[英]What would this Classic asp code look like in asp.net?

我必须将旧的经典ASP网站的某些逻辑转换为asp.net项目。 我在理解某些负责发布数据的功能时遇到了麻烦。

这是Classic ASP中的函数:

<%Function PostHTTP(strURL, strBody, strErrTemplate)
ON ERROR RESUME NEXT
Dim objHTTP, strResult

  Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")

  If Err.Number <> 0 Then
    strResult = Replace(strErrTemplate, "%1", Err.Number)
    strResult = Replace(strResult, "%2", Err.Description)
    strResult = Replace(strResult, "%3", "Init::" & Err.Source)
    Set objHTTP = Nothing
    PostHTTP = strstrResult
    Exit Function
  End If

  With objHTTP
    .Open "POST", strURL, False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .setTimeouts 30000, 30000, 60000, 240000
    .send strBody

    If Err.Number <> 0 Then
      strResult = Replace(strErrTemplate, "%1", Err.Number)
      strResult = Replace(strResult, "%2", Err.Description)
      strResult = Replace(strResult, "%3", "Post::" & Err.Source)
    Else
      strResult = .responseText
    End If
  End With

' Response.Write "strResult: " & strResult
'Response.End


  If Err.Number > 0 Then
    strResult = Replace(strErrTemplate, "%1", Err.Number)
    strResult = Replace(strResult, "%2", Err.Description)
    strResult = Replace(strResult, "%3", Err.Source)
  ElseIf Len(strResult) = 0 Then
    strResult = Replace(strErrTemplate, "%1", 2000)
    strResult = Replace(strResult, "%2", "No response received from remote server.")
    strResult = Replace(strResult, "%3", "PostHTTP")
  End If

  PostHTTP = strResult
  Set objHTTP = Nothing
End Function

在asp.net中会是什么样?

ps:我已经尝试了自己的发布功能,但显然错过了一些东西,因为我的行不通。

如果使用WebClient类,则POST的主要任务将非常简单。 例如,

// Form URL and POST-DATA
...
using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string strResult = wc.UploadString(strURL, strBody);
}
...

要进行更精细的控制,可以使用WebRequest类。

编辑 :这是WebRequest的示例代码,因为似乎您需要指定WebClient无法实现的超时值

var request = WebRequest.Create(strUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 240000; // set timeout
using (var writer = new StreamWriter(request.GetRequestStream()))
{
    // write to the body of the POST request
    writer.Write(strBody);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM