public List<DocProfile> GetDoctorsForType(System.Net.HttpWebRequest req, string profile_id) { var r = req.GetResponse(); var sr = new System.IO.StreamReader(r.GetResponseStream(), Encoding.GetEncoding(ENCODING)); string res = sr.ReadToEnd(); sr.Close(); r.Close(); var ret = new List<DocProfile>(); int i1 = res.IndexOf("timetable?doctor_id="); while (i1 != -1) { int i10 = res.IndexOf("&", i1); string doctor_id = res.Substring(i1 + "timetable?doctor_id=".Length, i10 - i1 - "timetable?doctor_id=".Length); int i2 = res.IndexOf("\">", i10); i2 = res.IndexOf("\">", i2 + 1) + "\">".Length; int i3 = res.IndexOf("</span>", i2); string name = res.Substring(i2, i3 - i2); name = System.Text.RegularExpressions.Regex.Replace(name, @"\s+", " "); int i4 = res.IndexOf("\">", i3) + "\">".Length; int i5 = res.IndexOf("</span>", i4); string desc = res.Substring(i4, i5 - i4); desc = System.Text.RegularExpressions.Regex.Replace(desc, @"\s+", " "); ret.Add(new DocProfile(profile_id, doctor_id, name, desc)); i1 = res.IndexOf("timetable?doctor_id=", i1 + 1); } return ret; }
private static System.Net.HttpWebResponse GetResponse(System.Net.HttpWebRequest req) { try { return (System.Net.HttpWebResponse)req.GetResponse(); } catch (System.Net.WebException wex) { return (System.Net.HttpWebResponse)wex.Response; } }
public string GetDocTimetable(System.Net.HttpWebRequest req, ref List<TimeRecord> timeRecords, out string docName, out string docSpec) { System.Net.WebResponse resp = req.GetResponse(); var sr = new System.IO.StreamReader(resp.GetResponseStream(), Encoding.GetEncoding(ENCODING)); string res = sr.ReadToEnd(); sr.Close(); resp.Close(); int i00 = res.IndexOf("Врач"); int i01 = res.IndexOf("<a href=\"/service/record/doctors\">", i00) + "<a href=\"/service/record/doctors\">".Length; int i02 = res.IndexOf("</a></li>", i01); docName = res.Substring(i01, i02 - i01); i00 = res.IndexOf("Специалист"); i01 = res.IndexOf("<a href=\"/service/record/profiles\">", i00) + "<a href=\"/service/record/profiles\">".Length; i02 = res.IndexOf("</a></li>", i01); docSpec = res.Substring(i01, i02 - i01); int i1 = res.IndexOf("<td id=\""); while (i1 != -1) { int i10 = res.IndexOf("\"", i1 + "<td id=\"".Length); string id = res.Substring(i1 + "<td id=\"".Length, i10 - i1 - "<td id=\"".Length); int i2 = res.IndexOf("rel=\"", i1) + "rel=\"".Length; int i3 = res.IndexOf("\" rel2=", i2); string dateTime = res.Substring(i2, i3 - i2); int i4 = res.IndexOf("alt=\"", i3) + "alt=\"".Length; int i5 = res.IndexOf("\">", i4); string type = res.Substring(i4, i5 - i4); timeRecords.Add(new TimeRecord(id, dateTime, type)); i1 = res.IndexOf("<td id=\"", i1 + 1); } return res; }
/// <summary> /// Gets the value of the named field parsed as date in milliseconds /// </summary> /// <param name="request">Instance of System.Net.HttpWebRequest to get the headers from</param> /// <param name="fieldName">Name of the header field</param> /// <param name="defaultValue">A default value to return if the value does not exist in the headers</param> /// <returns></returns> public static long GetHeaderFieldDate(System.Net.HttpWebRequest request, System.String fieldName, long defaultValue) { long fieldHeaderDate; try { fieldHeaderDate = System.DateTime.Parse(request.GetResponse().Headers.Get(fieldName)).Ticks; } catch(System.Exception) { fieldHeaderDate = defaultValue; } return fieldHeaderDate; }
/// <summary> /// Gets the value of the "Last-Modified" property from the collection of headers associated with the specified HttWebRequest /// </summary> /// <param name="request">Instance of HttpWebRequest to get the headers from</param> /// <returns>The value of the "Last-Modified" property if found, otherwise returns null</returns> public static long GetLastModifiedHeaderField(System.Net.HttpWebRequest request) { long fieldHeaderDate; try { fieldHeaderDate = System.DateTime.Parse(request.GetResponse().Headers.Get("Last-Modified")).Ticks; } catch(System.Exception) { fieldHeaderDate = 0; } return fieldHeaderDate; }
/// <summary> /// Gets the key for the specified index from the KeysCollection of the specified HttpWebRequest's Headers property /// </summary> /// <param name="request">Instance HttpWebRequest to get the key from</param> /// <param name="indexField">Index of the field to get the corresponding key</param> /// <returns>The key for the specified index if found, otherwise null</returns> public static System.String GetHeaderFieldKey(System.Net.HttpWebRequest request, int indexField) { try { return request.GetResponse().Headers.Keys.Get(indexField); } catch(System.Exception) {} return null; }
/// <summary> /// Gets the sending date of the resource referenced by the HttpRequest /// </summary> /// <param name="request">Instance of HttpWebRequest to get the date from</param> /// <returns>The sending date of the resource if found, otherwise 0</returns> public static long GetSendingDate(System.Net.HttpWebRequest request) { long headerDate; try { headerDate = System.DateTime.Parse(request.GetResponse().Headers.Get("Date")).Ticks; } catch(System.Exception) { headerDate = 0; } return headerDate; }
/// <summary> /// Gets the value of the "Content-Encoding" property from the collection of headers associated with the specified HttpWebRequest /// </summary> /// <param name="request">Instance of HttpWebRequest to get the headers from</param> /// <returns>The value of the "Content-Encoding" property if found, otherwise returns null</returns> public static System.String GetContentEncoding(System.Net.HttpWebRequest request) { try { return request.GetResponse().Headers.Get("Content-Encoding"); } catch(System.Exception) {} return null; }
/// <summary> /// Processes the response stream. /// </summary> /// <param name="req">The req.</param> /// <returns></returns> private string ProcessResponseStream(System.Net.HttpWebRequest req) { using (System.Net.WebResponse resp = req.GetResponse()) { if (resp == null) return null; // Update the domain we're now on Domain = resp.ResponseUri.Authority; LastProcessResponseUri = resp.ResponseUri; using (Stream stream = resp.GetResponseStream()) { using (System.IO.StreamReader sr = new System.IO.StreamReader(stream)) { return sr.ReadToEnd().Trim(); } } } }
/// <summary> /// Sends an assembled <c>request</c>. /// </summary> /// <returns> /// Any interesting properties and, depending on whether <c>output</c> is set, an attached stream. /// If <c>output</c> is provided, it is used instead. the or writing the result to that /// provided, seeking back to 0 in either case. /// </returns> /// <param name='request'> /// Request. /// </param> /// <param name='output'> /// Output. /// </param> /// <param name='timeout'> /// Timeout. /// </param> /// <exception cref="Exceptions.ProtocolError"> /// This, or one of this children, is thrown if a problem occurs while communicating with the server. /// </exception> /// <exception cref="Exceptions.URLError"> /// Thrown if the server cannot be contacted. /// </exception> internal static Response SendRequest(System.Net.HttpWebRequest request, System.IO.Stream output=null, float timeout=10.0f) { try{ request.GetRequestStream().Close(); Response r = new Response(); using(System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse()){ using(System.IO.Stream stream = response.GetResponseStream()){ r.Properties = new System.Collections.Generic.Dictionary<string, object>(); string applied_compression = response.Headers.Get(Communication.HEADER_APPLIED_COMPRESSION); if(applied_compression != null){ r.Properties.Add(Communication.PROPERTY_APPLIED_COMPRESSION, Compression.ResolveCompressionFormat(applied_compression)); }else{ r.Properties.Add(Communication.PROPERTY_APPLIED_COMPRESSION, null); } r.Properties.Add(Communication.PROPERTY_CONTENT_TYPE, response.ContentType); if(output != null){ stream.CopyTo(output); r.Properties.Add(Communication.PROPERTY_CONTENT_LENGTH, output.Length); output.Seek(0, System.IO.SeekOrigin.Begin); r.Data = output; }else{ r.Data = new System.IO.MemoryStream(); stream.CopyTo(r.Data); r.Data.Seek(0, System.IO.SeekOrigin.Begin); r.Properties.Add(Communication.PROPERTY_CONTENT_LENGTH, response.ContentLength); } } } return r; }catch(System.Net.WebException e){ if(e.Status == System.Net.WebExceptionStatus.ProtocolError && e.Response != null){ System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)e.Response; if(response.StatusCode == System.Net.HttpStatusCode.Forbidden){ throw new Exceptions.NotAuthorisedError("The requested operation could not be performed because an invalid key was provided"); }else if(response.StatusCode == System.Net.HttpStatusCode.NotFound){ throw new Exceptions.NotFoundError("The requested resource was not retrievable; it may have been deleted or not yet defined"); }else if(response.StatusCode == System.Net.HttpStatusCode.Conflict){ throw new Exceptions.InvalidRecordError("The uploaded request is structurally flawed and cannot be processed"); }else if(response.StatusCode == System.Net.HttpStatusCode.PreconditionFailed){ throw new Exceptions.InvalidHeadersError("One or more of the headers supplied (likely Content-Length) was rejected by the server"); }else if(response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable){ throw new Exceptions.TemporaryFailureError("The server was unable to process the request"); } throw new Exceptions.ProtocolError("Unable to send message; code: " + response.StatusCode, e); }else if(e.Status == System.Net.WebExceptionStatus.ConnectFailure || e.Status == System.Net.WebExceptionStatus.NameResolutionFailure){ throw new Exceptions.URLError("Unable to send message: " + e.Message, e); } throw; } }
public List<DocTypeId> GetDoctorsTypes(System.Net.HttpWebRequest req) { var r = req.GetResponse(); var sr = new System.IO.StreamReader(r.GetResponseStream(), Encoding.GetEncoding(ENCODING)); string res = sr.ReadToEnd(); sr.Close(); r.Close(); var ret = new List<DocTypeId>(); int i1 = res.IndexOf("hospitals?profile_id="); while (i1 != -1) { int i2 = res.IndexOf("\">", i1); string profile_id = res.Substring(i1 + "hospitals?profile_id=".Length, i2 - i1 - "hospitals?profile_id=".Length); string type; if (res.Substring(i2 + 35, 5) == "<span") { int i3 = res.IndexOf("\">", i2 + 35) + "\">".Length; int i4 = res.IndexOf("</span>", i3); type = res.Substring(i3, i4 - i3); } else { int i3 = res.IndexOf("</a></li>", i2 + 2); type = res.Substring(i2 + 2, i3 - i2 - 2); } ret.Add(new DocTypeId(type, profile_id)); i1 = res.IndexOf("hospitals?profile_id=", i1 + 1); } return ret; }
private System.Net.HttpWebResponse GetResponse(System.Net.HttpWebRequest request, int retries) { try { request.Timeout = 1200000; return (System.Net.HttpWebResponse)request.GetResponse(); } catch (Exception exception4) { if (retries < 1) { throw exception4; } retries = retries - 1; return this.GetResponse(request, retries); } }