Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search XML API and .NET

Status
Not open for further replies.

blhrtp

Programmer
Jul 11, 2003
2
US
Hi,

I am trying to use Livelink's Search XML API in VB.NET, in ASP.NET.

I can paste the following query URI in a browser and the search results are returned and displayed in valid, well formed XML in my browser:


However, I want to return the XML and store it in an XML Document in vb.net code behind an aspx page. The code below runs successfully, but the string returned from the http web request is html from a "redirection", instead of the XML search results.

Dim HttpWReq As HttpWebRequest
Dim HttpWResp As HttpWebResponse
Dim ReturnedData As String
Dim srTemp As StreamReader

If Not IsPostBack Then
Dim xmlResults As New XmlDocument()

HttpWReq = CType(WebRequest.Create(" HttpWebRequest)

HttpWReq.Credentials = CredentialCache.DefaultCredentials
'HttpWReq.ContentType = "text/xml"
HttpWReq.Method = "GET"

HttpWResp = CType(HttpWReq.GetResponse(), HttpWebResponse)
If HttpWResp.StatusCode <> HttpStatusCode.OK Then
ReturnedData = New String(&quot;&quot;)
Else
'xmlResults.Load(HttpWResp.GetResponseStream())
'xmlResults.Save(&quot;\\us00011540\junk\test2.txt&quot;)

srTemp = New StreamReader(HttpWResp.GetResponseStream())
ReturnedData = srTemp.ReadToEnd()
Dim StreamLength As Int64 = ReturnedData.Length()

If StreamLength > 0 Then
Dim objReader = New StreamWriter(&quot;\\server\Junk\test.txt&quot;)
objReader.Write(ReturnedData)
objReader.Close()
End If
HttpWResp.Close()
End If
End If

Like I say, the code runs, but ReturnedData ends up being the following:

<HTML>
<!-- File: redirectmeta.html -->
<HEAD>
<TITLE>Livelink - Redirection</TITLE>
<META HTTP-EQUIV=&quot;Refresh&quot; CONTENT=&quot;0; URL=/us_ll9103tst/livelink.exe?func=search&lookfor1=allwords&where1=report&outputformat=xml&quot;>
</HEAD>
</HTML>
<!-- End File: redirectmeta.html -->

I'm wanting to get the search results in XML format, as they show up if I paste my URI into a browser.

Any help is appreciated.
BLH
 
Did u get any success with this problem....
I am having the same problem.ALthough my code written in aspx page wroks OK yet when I call the same class from a WIndows Form it fails

Thanks in advance...
 
Yes, I did solve this problem with a co-workers help. Basically, you have to send your request at least 2 times. The first time you get cookie information back, which you have to append to your second request, which then gives you the XML output from the search. Here is my sample code, including some unnecessary debugging stuff:

Dim HReq As HttpWebRequest
Dim HReq2 As HttpWebRequest
Dim HRes As HttpWebResponse
Dim ReturnedData As String
Dim srTemp As StreamReader

If Not IsPostBack Then
Dim xmlResults As New XmlDocument()

HReq = CType(WebRequest.Create(&quot; HttpWebRequest)

HReq.Credentials = CredentialCache.DefaultCredentials
HReq.Method = &quot;GET&quot;

Dim cookCont As New CookieContainer()
Dim i As Int16
Dim headColl As New WebHeaderCollection()
Dim cookCol As New CookieCollection()
Dim cook As New Cookie()
Dim newUri As New Uri(&quot;
Dim NameValColl As NameValueCollection

HReq.CookieContainer = cookCont
HRes = CType(HReq.GetResponse(), HttpWebResponse)

Response.Write(&quot;Request Cookies Count: &quot; & cookCont.Count & &quot;<br/>&quot;)
cookCol = cookCont.GetCookies(newUri)
For Each cook In cookCol
Response.Write(cook.Name() & &quot;<br/>&quot;)
Response.Write(cook.Path() & &quot;<br/>&quot;)
Response.Write(cook.Value() & &quot;<br/><br/>&quot;)
Next

i = 0
headColl = HRes.Headers
NameValColl = HRes.Headers
Response.Write(&quot;Response Headers Count: &quot; & headColl.Count & &quot;<br/>&quot;)
For i = 0 To headColl.Count - 1
Response.Write(headColl.Item(i).ToString & &quot;<br/><br/>&quot;)
Next

'*****Here is the second request*************
HReq2 = CType(WebRequest.Create(&quot; HttpWebRequest)
HReq2.CookieContainer = cookCont
HReq2.Credentials = CredentialCache.DefaultCredentials
HReq2.ContentType = &quot;text/xml&quot;
HReq2.Method = &quot;GET&quot;

HReq2.CookieContainer = cookCont
HRes = CType(HReq2.GetResponse(), HttpWebResponse)

For Each cook In cookCol
Response.Write(cook.Name() & &quot;<br/>&quot;)
Response.Write(cook.Path() & &quot;<br/>&quot;)
Response.Write(cook.Value() & &quot;<br/><br/>&quot;)
Next

i = 0
headColl = HRes.Headers
NameValColl = HRes.Headers
Response.Write(&quot;Response Headers Count: &quot; & headColl.Count & &quot;<br/>&quot;)
For i = 0 To headColl.Count - 1
Response.Write(headColl.Item(i).ToString & &quot;<br/><br/>&quot;)
Next

If Not (HRes Is Nothing) Then
If Response.StatusCode = HttpStatusCode.Unauthorized Then
Dim challenge As String = Nothing
challenge = HRes.GetResponseHeader(&quot; If Not (challenge Is Nothing) Then
Response.Write(&quot;The following challenge was raised by the server: &quot; & challenge)
End If
End If
End If

If HRes.StatusCode <> HttpStatusCode.OK Then

ReturnedData = New String(&quot;&quot;)
Else
srTemp = New StreamReader(HRes.GetResponseStream())
ReturnedData = srTemp.ReadToEnd()
Dim StreamLength As Int64 = ReturnedData.Length()
If StreamLength > 0 Then
Dim objReader = New StreamWriter(&quot;\\server\dir\z_search_results.xml&quot;)
objReader.Write(ReturnedData)
objReader.Close()
xmlResults.LoadXml(ReturnedData)
Dim aNode As XmlNode
aNode = xmlResults.SelectSingleNode(&quot;searchresult&quot;)
Response.Write(&quot;RootNode: &quot; & aNode.Name)
End If
HRes.Close()
End If
End If
 
The thread at thread862-616647 covers how to pass the login stuff as part of the URl ,so you will only need one request.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top