Forum » Programska oprema » C# in internetna stran
C# in internetna stran
cikopero ::
Živijo,
delam aplikacijo, ki bi komunicirala z internetno stranjo COBISS. Do sedaj sem prišel do naslednjega:
Našel sem primer, kako upravljati s spletno stranjo:
Sam sem uporabljal:
Težava je, ker je posebej vodena posamezna seja - različni link. V kodi strani najdem link, kamor kaže gumb za iskanje.
Ko uporabim povezavo, na katerega kaže gumb iskanje, mi sporoči, da je potrebno narediti novo sejo - zadnji result.
Morda kaka ideja, kako se lotiti problema. Sam imam le malo izkušenj s spletnimi stranmi, ravno tako šele dobro spoznavam C#.
Hvala za ideje,
Lep pozdrav Peter
delam aplikacijo, ki bi komunicirala z internetno stranjo COBISS. Do sedaj sem prišel do naslednjega:
Našel sem primer, kako upravljati s spletno stranjo:
public class PostSubmitter { /// <summary> /// determines what type of post to perform. /// </summary> public enum PostTypeEnum { /// <summary> /// Does a get against the source. /// </summary> Get, /// <summary> /// Does a post against the source. /// </summary> Post } private string m_url=string.Empty; private NameValueCollection m_values=new NameValueCollection(); private PostTypeEnum m_type=PostTypeEnum.Get; /// <summary> /// Default constructor. /// </summary> public PostSubmitter() { } /// <summary> /// Constructor that accepts a url as a parameter /// </summary> /// <param name="url">The url where the post will be submitted to.</param> public PostSubmitter(string url):this() { m_url=url; } /// <summary> /// Constructor allowing the setting of the url and items to post. /// </summary> /// <param name="url">the url for the post.</param> /// <param name="values">The values for the post.</param> public PostSubmitter(string url, NameValueCollection values):this(url) { m_values=values; } /// <summary> /// Gets or sets the url to submit the post to. /// </summary> public string Url { get { return m_url; } set { m_url=value; } } /// <summary> /// Gets or sets the name value collection of items to post. /// </summary> public NameValueCollection PostItems { get { return m_values; } set { m_values=value; } } /// <summary> /// Gets or sets the type of action to perform against the url. /// </summary> public PostTypeEnum Type { get { return m_type; } set { m_type=value; } } /// <summary> /// Posts the supplied data to specified url. /// </summary> /// <returns>a string containing the result of the post.</returns> public string Post() { StringBuilder parameters=new StringBuilder(); for (int i=0;i < m_values.Count;i++) { EncodeAndAddItem(ref parameters,m_values.GetKey(i),m_values[i]); } string result=PostData(m_url,parameters.ToString()); return result; } /// <summary> /// Posts the supplied data to specified url. /// </summary> /// <param name="url">The url to post to.</param> /// <returns>a string containing the result of the post.</returns> public string Post(string url) { m_url=url; return this.Post(); } /// <summary> /// Posts the supplied data to specified url. /// </summary> /// <param name="url">The url to post to.</param> /// <param name="values">The values to post.</param> /// <returns>a string containing the result of the post.</returns> public string Post(string url, NameValueCollection values) { m_values=values; return this.Post(url); } /// <summary> /// Posts data to a specified url. Note that this assumes that you have already url encoded the post data. /// </summary> /// <param name="postData">The data to post.</param> /// <param name="url">the url to post to.</param> /// <returns>Returns the result of the post.</returns> private string PostData(string url, string postData) { HttpWebRequest request=null; if (m_type==PostTypeEnum.Post) { Uri uri = new Uri(url); request = (HttpWebRequest) WebRequest.Create(uri); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; //request.ContentType = "text/html; charset=utf-8"; request.ContentLength = postData.Length; using(Stream writeStream = request.GetRequestStream()) { UTF8Encoding encoding = new UTF8Encoding(); byte[] bytes = encoding.GetBytes(postData); writeStream.Write(bytes, 0, bytes.Length); } } else { Uri uri = new Uri(url + "?" + postData); request = (HttpWebRequest) WebRequest.Create(uri); request.Method = "GET"; } string result=string.Empty; using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader readStream = new StreamReader (responseStream, Encoding.UTF8)) { result = readStream.ReadToEnd(); } } } return result; } /// <summary> /// Encodes an item and ads it to the string. /// </summary> /// <param name="baseRequest">The previously encoded data.</param> /// <param name="dataItem">The data to encode.</param> /// <returns>A string containing the old data and the previously encoded data.</returns> private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem) { if (baseRequest==null) { baseRequest=new StringBuilder(); } if (baseRequest.Length!=0) { baseRequest.Append("&"); } baseRequest.Append(key); baseRequest.Append("="); baseRequest.Append(HttpUtility.UrlEncode(dataItem)); } }
Sam sem uporabljal:
PostSubmitter post = new PostSubmitter(); //post.Url = "http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en"; post.Url = "http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=si"; post.Type = PostSubmitter.PostTypeEnum.Get; result = post.Post(); //dobimo ID seje int iIndex = result.IndexOf("http://cobiss4.izum.si/scripts/cobiss?id="); string newUrl = null; if (iIndex != -1) { for (int i = 0; i < result.Length; i++) { if (result[i + iIndex] == '\"') break; newUrl += result[i + iIndex]; } } else { iIndex = result.IndexOf("http://cobiss2.izum.si/scripts/cobiss?id="); if (iIndex != -1) { for (int i = 0; i < result.Length; i++) { if (result[i + iIndex] == '\"') break; newUrl += result[i + iIndex]; } } } //to je naslov, na katerega kaže gumb za iskanje knjižnice result = newUrl; if (newUrl != null) { post.Url = newUrl; //poiskali bomo knjiznice, ki ima v imenu FE post.PostItems.Add("bstr", "FE"); post.Type = PostSubmitter.PostTypeEnum.Post; result = post.Post(); } //DobljeniString.Text = result;
Težava je, ker je posebej vodena posamezna seja - različni link. V kodi strani najdem link, kamor kaže gumb za iskanje.
Ko uporabim povezavo, na katerega kaže gumb iskanje, mi sporoči, da je potrebno narediti novo sejo - zadnji result.
Morda kaka ideja, kako se lotiti problema. Sam imam le malo izkušenj s spletnimi stranmi, ravno tako šele dobro spoznavam C#.
Hvala za ideje,
Lep pozdrav Peter
c00L3r ::
Ti morda to kaj pomaga? Sam se mislim v prihodnosti lotit enega podobnega projekta. Moral bom prebrat podatke iz določene spletne strani, kjer moram bit login-an. Sporoči kako je šlo.
cikopero ::
Ti morda to kaj pomaga? Sam se mislim v prihodnosti lotit enega podobnega projekta. Moral bom prebrat podatke iz določene spletne strani, kjer moram bit login-an. Sporoči kako je šlo.
Hvala za nasvet,
Na hitro sem pogledal, vendar brez uspeha. Se bom še malo bolj poglobil v tej smeri. Če mi uspe, sporočim.
Lp,
Peter
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | Davčne blagajne (strani: 1 2 3 4 … 24 25 26 27 )Oddelek: Programiranje | 332501 (72504) | Macketina |
» | [PHP]Zajem slike iz direktorijaOddelek: Programiranje | 1231 (1148) | KernelPanic |
» | [C# asp.NET web forma] Kam zgine cookie oz. zkj se izbriše?Oddelek: Programiranje | 1116 (930) | Morenov |
» | ASP.NET + C# vprašanjeOddelek: Programiranje | 2767 (1781) | Morenov |
» | C#; WebClient class; problem z uploadanjemOddelek: Programiranje | 1421 (1380) | 64202 |