Hvordan kan jeg lave det sådan så at et URL bliver sendt ligesom hvis man, i sin browser, skriver et URL ind, og trykker enter. Hvordan kan jeg gøre dette med C#, så softwaren åbner det websted.
Meningen er at siden den åbner skal loade dens egen URL, fordi den indeholder variabler som den skal læse. som fx. index.php?pagecolor=red.
Men i det her tilfælde er det så noget andet den skal læse, sende til en MySQL server, og så skal en anden side loade det er content.
jeg har fixet alt det med PHP men når jeg bruger følgende kode i min C# software kommer contentet ikke frem på content siden.
- namespace MCQuai
- {
- public static class Heartbeat
- {
- //static int _timeout = 60 * 1000;
-
- static string hash;
- static string staticVars;
- static string newName = Server.name.Replace("\'", "\\\'");
-
- //static BackgroundWorker worker;
- static HttpWebRequest request;
- static System.Timers.Timer heartbeatTimer = new System.Timers.Timer(500);
-
- public static void Init()
- {
- staticVars = "port=" + Server.port +
- "&max=" + Server.players +
- "&name=" + UrlEncode(newName) +
- "&public=" + Server.pub +
- "&version=" + Server.version;
-
- Thread backupThread = new Thread(new ThreadStart(delegate
- {
- heartbeatTimer.Elapsed += delegate
- {
- heartbeatTimer.Interval = 55000;
- try
- {
- Pump(Beat.Minecraft);
- Pump(Beat.MCQuai);
- }
- catch (Exception e) { Server.ErrorLog(e); }
- };
- heartbeatTimer.Start();
- }));
- backupThread.Start();
- }
-
- public static bool Pump(Beat type)
- {
- string postVars = staticVars;
-
- string url = "http://www.minecraft.net/heartbeat.jsp";
- int totalTries = 0;
- retry: try
- {
- int hidden = 0;
- foreach (Player all in Player.players) { if(all.hidden) hidden++; }
- totalTries++;
- // append additional information as needed
- switch (type)
- {
- case Beat.Minecraft:
- postVars += "&salt=" + Server.salt;
- goto default;
- case Beat.MCQuai:
- postVars = "sname=" + newName
- + "&maxp=" + Server.players
- + "&nowp=" + (Player.number - hidden)
- + "&owner=" + Server.TheOwner
- + "&desc=" + File.ReadAllLines("text\\server description.txt")[0]
- + "&public=" + Server.pub
- + "&version=" + Server.Version
- + "&URL=" + Server.URL.Trim();
- url = "http://www.quaisaq.dk/phpTest/index.php";
- break;
- default:
- postVars += "&users=" + (Player.number - hidden);
- break;
-
- }
- request = (HttpWebRequest)WebRequest.Create(new Uri(url));
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
- byte[] formData = Encoding.ASCII.GetBytes(postVars);
- request.ContentLength = formData.Length;
- request.Timeout = 15000;
-
- retryStream: try
- {
- using (Stream requestStream = request.GetRequestStream())
- {
- requestStream.Write(formData, 0, formData.Length);
- requestStream.Close();
- }
- }
- catch (WebException e)
- {
- if (e.Status == WebExceptionStatus.Timeout)
- {
- goto retryStream;
- }
- }
-
- using (WebResponse response = request.GetResponse())
- {
- using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
- {
- if (hash == null)
- {
- hash = responseReader.ReadToEnd().Substring(responseReader.ReadToEnd().LastIndexOf('=') + 1).Trim();
- Server.URL = hash.Substring(hash.IndexOf('=') + 1);
- if (hash == "")
- {
- Server.s.Log("Server was not able to recieve the URL");
- hash = null;
- }
- else if (hash.Contains("<"))
- {
- hash = "";
- Server.URL = "";
- Server.s.Log("It seems that minecraft.net is offline.");
- Server.s.Log("Because of this, your server is not accessible,");
- Server.s.Log("untill minecraft.net is back online.");
- hash = null;
- }
- Server.s.UpdateUrl(hash);
- if (hash != null) File.WriteAllText("text\\externalurl.txt", hash);
- }
- }
- }
- }
- catch (WebException e)
- {
- if (e.Status == WebExceptionStatus.Timeout)
- {
- Pump(type);
- }
- }
- catch
- {
- if (totalTries < 3) goto retry;
- return false;
- }
- finally
- {
- request.Abort();
- }
- return true;
- }
-
- public static string UrlEncode(string input)
- {
- StringBuilder output = new StringBuilder();
- for (int i = 0; i < input.Length; i++)
- {
- if ((input[i] >= '0' && input[i] <= '9') ||
- (input[i] >= 'a' && input[i] <= 'z') ||
- (input[i] >= 'A' && input[i] <= 'Z') ||
- input[i] == '-' || input[i] == '_' || input[i] == '.' || input[i] == '~')
- {
- output.Append(input[i]);
- }
- else if (Array.IndexOf<char>(reservedChars, input[i]) != -1)
- {
- output.Append('%').Append(((int)input[i]).ToString("X"));
- }
- }
- return output.ToString();
- }
- public static char[] reservedChars = { ' ', '!', '*', '\'', '(', ')', ';', ':', '@', '&',
- '=', '+', '$', ',', '/', '?', '%', '#', '[', ']' };
- }
-
- public enum Beat { Minecraft, MCQuai }
- }
her bliver den sendt hen:
- <html>
- <head><title>Heartbeat of MCQuai</title></head>
- <body>
- <?php
- $con = mysql_connect("localhost","quaisaq_dk","******");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
-
- $working = true;
- $sname = $_GET['sname'];
- $maxp = $_GET['maxp'];
- $nowp = $_GET['nowp'];
- $owner = $_GET['owner'];
- $desc = $_GET['desc'];
- $version = $_GET['version'];
- $public = $_GET['public'];
- $URL = $_GET['URL'];
- if ($sname == "" || $maxp == "" || $nowp == "" || $URL == "")
- $working = false;
- if ($public == "") $public = 1;
- if ($owner == "") $owner = "Unknown";
- if ($desc == "") $desc = "A nice server.";
-
- $date_array = getdate();
- $date = $date_array[year] . "-" . $date_array[mon] . "-" . $date_array[mday] . " " . $date_array[hours] . ":" . $date_array[minutes] . ":" . $date_array[seconds];
- $time = strtotime($date . "+3 minutes");
- $datetime = date('Y-m-d H:i:s', $time);
- print "<br>Datetime: " . $datetime . "<br><br>";
-
- if ($working)
- {
-
- // check/create/insert table
- mysql_select_db("DB", $con);
-
- $new = true;
- $result = mysql_query("SELECT * FROM mcquai_Heartbeat");
- while($row = mysql_fetch_array($result))
- {
- if($row['URL'] == $URL) { $new = false; }
- }
- if ($new == true)
- {
- $sql="INSERT INTO mcquai_Heartbeat (ServerName, MaxPlayers, Playing, Owner, Description, Version, Public, URL, Expire)
- VALUES('$sname', '$maxp', '$nowp', '$owner', '$desc', '$version', '$public', '$URL', '$datetime')";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- }
-
- $sql="UPDATE mcquai_Heartbeat SET ServerName = '$sname'
- WHERE URL = '$URL'";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- $sql="UPDATE mcquai_Heartbeat SET MaxPlayers = '$maxp'
- WHERE URL = '$URL'";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- $sql="UPDATE mcquai_Heartbeat SET Playing = '$nowp'
- WHERE URL = '$URL'";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- $sql="UPDATE mcquai_Heartbeat SET Owner = '$owner'
- WHERE URL = '$URL'";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- $sql="UPDATE mcquai_Heartbeat SET Description = '$desc'
- WHERE URL = '$URL'";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- $sql="UPDATE mcquai_Heartbeat SET Public = '$public'
- WHERE URL = '$URL'";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- $sql="UPDATE mcquai_Heartbeat SET Expire = '$datetime'
- WHERE URL = '$URL'";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
-
- echo "Server Listed!";
- }
- else echo "Invalid URL";
-
- mysql_close($con);
- ?>
- </body>
- </html>
content viseren:
- $con = mysql_connect("localhost","quaisaq_dk","******");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db("quaisaq_dk", $con);
- $date_array = getdate();
- $date = $date_array[year] . "-" . $date_array[mon] . "-" . $date_array[mday] . " " . $date_array[hours] . ":" . $date_array[minutes] . ":" . $date_array[seconds];
- $today = strtotime($date);
- $result = mysql_query("SELECT * FROM mcquai_Heartbeat");
-
- while($row = mysql_fetch_array($result))
- {
- if (strtotime($row['Expire']) < $today)
- {
- $u = $row['URL'];
- $e = $row['Expire'];
- $sql="DELETE FROM mcquai_Heartbeat WHERE Expire='$e'";
- if (!mysql_query($sql,$con))
- {
- die('<br>Error: ' . mysql_error());
- }
- }
- }
- echo "<center><br>";
- $result = mysql_query("SELECT * FROM mcquai_Heartbeat");
- while($row = mysql_fetch_array($result))
- {
- $sname = $row['ServerName'];
- $maxp = $row['MaxPlayers'];
- $nowp = $row['Playing'];
- $owner = $row['Owner'];
- $desc = $row['Description'];
- $URL = $row['URL'];
- echo "<div class=\"server\">";
- echo "<p style=\"text-align: center;\"><a href=\"http://www.minecraft.net/play.jsp?server=$URL\" style=\"font-size: large;\">$sname</a></p>";
- echo "<table width=\"300\"><tr>";
- echo "<td style=\"color: white;\">$nowp / $maxp</p></p></td>";
- echo "<td>|</td>";
- echo "<td style=\"color: white; text-align: right;\">$owner</td>";
- echo "</tr></table>";
- echo "<table><tr><td width=\"10\"></td><td style=\"color:white;\">" . $desc . "</td><td width=\"10\"></td></tr></table>";
- echo "</div>";
- }
- echo "</center>";
- echo "</div>";
- mysql_close($con);
som du muligvis lægger mærke til, så er det ikke alt den sendte kode der bliver brugt, det er fordi jeg ikke er færdi med det visuelle.
Men jeg vil stadig gerne finde ud af hvorfor den ikke får sendt det der.
Det har nemlig virket før. Men nu er vi flere på holdet om at lave det her, og jeg ved ikke om nogen har ødelagt et eller andet herinde. Jeg er egentligt også ligeglad, bare det bliver fixet =/
Det skal måske lige siges at Beat.Minecraft bliver sendt korekt afsted.
Det er Beat.MCQuai som fejler.
Indlæg senest redigeret d. 11.06.2011 12:21 af Bruger #16191