在 .NET 中,默认情况下,对于服务器端控件,客户端 ID 与生成的文本连接。
例如:
<asp:TextBox ID="txtUser" runat="server">
会成为...
<input type="text" id="ctl00_body_txbUser">
但是,当我使用HttpWebRequest.GetResponse(objReq.Getresponse, HttpWebResponse)请求同一页面时,该项目返回时没有自动生成的文本。
<input type="text" id="txbUser">
是否可以使用 HttpWebRequest 对象和 GetResponse 以使其返回带有自动生成的 ID 的 .NET 用于服务器端控件的响应?
我正在与以前设置特定于 ID 的翻译规则的第 3 方合作,现在我们正在尝试针对 API 调用使用相同的规则,该调用传递了从页面生成的字符串。但是,从页面生成的字符串没有相同的 ID。
下面是用于将网页作为字符串返回的代码。
Public Shared Function GetWebPageAsString(ByVal strURI As String, ByVal strPostData As String) As String
' Declare our variables. '
Dim objHttpRequest As HttpWebRequest
Dim PostBuffer() As Byte
Dim PostDataStream As Stream = Nothing
Dim objHttpResponse As HttpWebResponse = Nothing
Dim objStreamReader As StreamReader = Nothing
Dim strResponseText As String = ""
Try
' Create a new request. '
objHttpRequest = CType(WebRequest.Create(strURI), HttpWebRequest)
objHttpRequest.Timeout = 3000000
objHttpRequest.Method = "POST"
PostBuffer = Encoding.ASCII.GetBytes(strPostData)
objHttpRequest.ContentType = "application/x-www-form-urlencoded"
objHttpRequest.ContentLength = PostBuffer.Length
PostDataStream = objHttpRequest.GetRequestStream
PostDataStream.Write(PostBuffer, 0, PostBuffer.Length)
PostDataStream.Close()
' Get the response to our request as a stream object. '
objHttpResponse = CType(objHttpRequest.GetResponse, HttpWebResponse)
' Create a stream reader to read the data from the stream. '
objStreamReader = New StreamReader(objHttpResponse.GetResponseStream, Encoding.UTF8)
' Copy the text retrieved from the stream to a variable. '
strResponseText = objStreamReader.ReadToEnd()
' Close our objects. '
objStreamReader.Close()
objHttpResponse.Close()
Catch ex As Exception
strResponseText = strURI & " | " & strPostData
Throw (ex)
Finally
If Not objStreamReader Is Nothing Then
objStreamReader.Close()
End If
If Not PostDataStream Is Nothing Then
PostDataStream.Close()
End If
If Not objHttpResponse Is Nothing Then
objHttpResponse.Close()
End If
objHttpRequest = Nothing
PostBuffer = Nothing
PostDataStream = Nothing
objHttpResponse = Nothing
objStreamReader = Nothing
End Try
' Set return value. '
Return strResponseText
End Function
编辑:为了澄清,我需要 ID 继续由 .NET 自动生成。我知道我可以通过将模式设置为静态来使它们相等。不幸的是,我们正在使用的第 3 方已经根据 .NET 生成的 ID 为我们当前的页面创建了规则。使用 HTTPRequest 对象请求同一页面并将数据推送到流中。我不再看到自动生成的 ID,即使它是同一页。