Example #1
0
File: oai.cs Project: reeset/oai.cs
		public ListRecord ListRecords(string sPrefix, 
			string sset, 
			string sfrom, 
			string suntil, 
			ResumptionToken objToken, 
			ref Object objHandler) 
		{
            int fail = 0;
            Restart:
			System.IO.Stream objStream;
			ListRecord objList = new ListRecord();
			Record objRecord;
			ResumptionToken myToken;
			string tmp = "";
			System.Net.HttpWebRequest wr = null;
            System.Xml.XmlTextReader rd = null;

			if (sPrefix.Length==0) 
			{
				sPrefix = "oai_dc";
			}

			if (objToken==null) 
			{
				if (sset.Length!=0) 
				{
					sset = "&set=" + sset;
				} 
				if (sfrom.Length!=0) 
				{
					sfrom = "&from=" + sfrom;
				}
				if (suntil.Length!=0) 
				{
					suntil = "&until=" + suntil;
				}

				prequestURL = baseURL + "?verb=ListRecords&metadataPrefix=" + sPrefix + sset + sfrom + suntil;

			} 
			else 
			{
				//This is where we handle the resumptionToken - McCown
				prequestURL = baseURL + "?verb=ListRecords&resumptionToken=" + objToken.resumptionToken;
			}
			//======================================================
			// If you wanted to support additional metadata formats, 
			// you would just need to have additional handlers.
			//======================================================
            try
            {
                wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(prequestURL);
                //wr.Headers.Add("Host:" + System.Net.Dns.GetHostName());
                wr.UserAgent = cUserAgent + DateTime.Now.ToString();

                if (this.Timeout <= 0) { this.Timeout = 100000; }
                wr.Timeout = this.Timeout;

                System.Net.WebResponse response = wr.GetResponse();
                objStream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(objStream);
                prawXML = reader.ReadToEnd();
                //System.Windows.Forms.MessageBox.Show(prawXML);
                reader.Close();
                response.Close();
                rd = new System.Xml.XmlTextReader(prawXML, System.Xml.XmlNodeType.Document, null);
                rd.Normalization = true;
            }
            catch (System.Net.WebException ee)
            {
                int sleepval = 3000;
                
                fail++;
                //System.Windows.Forms.MessageBox.Show(tt.ToString());
                //System.Windows.Forms.MessageBox.Show(ee.ToString());
                if (ee.Status == System.Net.WebExceptionStatus.ProtocolError)
                {
                    var response = ee.Response as System.Net.HttpWebResponse;
                    if (response != null)
                    {
                        if ((int)response.StatusCode == 503)
                        {
                            string retryafter = response.Headers["Retry-After"];
                            if (retryafter != null && IsNumeric(retryafter) == true)
                            {
                                {
                                    sleepval = System.Convert.ToInt32(retryafter) * 1000;
                                }
                            }
                        }
                    }

                    if (fail <= 4)
                    {
                        //System.Windows.Forms.MessageBox.Show(sleepval.ToString());
                        System.Threading.Thread.Sleep(sleepval);
                        goto Restart;
                    }
                    else
                    {
                        wr.Abort();
                        error.errorName = ee.ToString();
                        error.errorDescription = ee.Message + "\n<br>Unable to connect to " + baseURL;
                        return null;
                    }
                }
            }
            catch (Exception e)
            {
               // System.Windows.Forms.MessageBox.Show("2");
                if (wr != null)
                {
                    wr.Abort();
                }
                error.errorName = e.ToString();
                error.errorDescription = e.Message + "\n<br>Unable to connect to " + baseURL;
                return null;
            }

            fail = 0;
			while (rd.Read()) 
			{
				if (rd.NodeType == System.Xml.XmlNodeType.Element) 
				{
					if (rd.Name == "responseDate")
					{
						presponseDate = rd.ReadString();
					}
					else if (rd.Name=="ListRecords") 
					{ 
						do 
						{
                            
							if (rd.Name=="record") 
							{
								tmp = ParseOAIContainer(ref rd, "record");
								objRecord = new Record(tmp,ref objHandler);
								objList.record.Add(objRecord);
								//return objRecord;
							} 
							else if (rd.Name=="resumptionToken") 
							{
								tmp=rd.ReadOuterXml();
								myToken = new ResumptionToken(tmp);
								objList.token = myToken;
							}
                            //else if (rd.Name.ToLower() == "OAI-PMH".ToLower())
                            //{
                            //    break;
                            //}
                            else if (rd.EOF == true)
                            {
                                error.errorName = "Empty ListRecords Request";
                                error.errorDescription = "No data was returned in the ListRecords Element.  This is likely an error.";
                                return null;
                            }
                            else rd.Read(); // Added the Read() and will never occur with the ReadInnerXml()

						} while (rd.Name!="ListRecords"); // loop
					} 
					else if (rd.Name=="error") 
					{
						error.errorName = rd.GetAttribute("code");
						error.errorDescription = rd.ReadString();
						return null;
					}
				}
			}

            
            return objList;
            

			
		}
Example #2
0
File: oai.cs Project: reeset/oai.cs
		public Record GetRecord(string sidentifier, string sPrefix, ref Object objHandler) 
		{
			System.IO.Stream objStream;
			Record objRecord;
			string tmp = "";
			System.Net.HttpWebRequest wr;
			System.Xml.XmlTextReader rd;

			if (sPrefix.Length==0) 
			{
				sPrefix = "oai_dc";
			}

			prequestURL = baseURL + "?verb=GetRecord&metadataPrefix=" + sPrefix + "&identifier=" + sidentifier;

			//======================================================
			// If you wanted to support additional metadata formats, 
			// you would just need to have additional handlers.
			//======================================================

			try 
			{
				wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(prequestURL);
				wr.UserAgent = cUserAgent;
				System.Net.WebResponse response = wr.GetResponse();
				objStream = response.GetResponseStream();
				System.IO.StreamReader reader = new System.IO.StreamReader(objStream);
				prawXML = reader.ReadToEnd();
				reader.Close();
				rd = new System.Xml.XmlTextReader(prawXML, System.Xml.XmlNodeType.Document, null);
			}
			catch (Exception e)
			{
                
				error.errorName = e.ToString();
				error.errorDescription = e.Message + "\n<br>Unable to connect to " + baseURL; 
				return null;
			}
			
			while (rd.Read()) 
			{
				if (rd.NodeType == System.Xml.XmlNodeType.Element) 
				{
					if (rd.Name == "responseDate")
					{
						presponseDate = rd.ReadString();
					}
					else if (rd.Name=="GetRecord") 
					{ 
						do
						{
							if (rd.Name=="record") 
							{
								tmp = ParseOAIContainer(ref rd, "record");
								objRecord = new Record(tmp, ref objHandler);
								return objRecord;
							}
							else rd.Read(); // Added the Read() and will never occur with the ReadInnerXml()

						} while (rd.Name!="GetRecord"); // loop
					} 
					else if (rd.Name=="error") 
					{
						error.errorName = rd.GetAttribute("code");
						error.errorDescription = rd.ReadString();
						return null;
					}
				}
			}

			return null;

		}