I'm totally new to the asp.net mvc stack and i was wondering what happened to the Request.Header object?
Basically what I want to do is to pull out the Device(PC) IP address, but I fail to retrieve the desired output. I also tried Request.ServerVariables object but result always remain NULL.
I am using asp.net MVC. is there any change in this function required:
public static string GetIP()
{
string functionReturnValue = null;
//Gets IP of actual device versus the proxy (WAP Gateway)
functionReturnValue = HttpContext.Current.Request.Headers["X-FORWARDED-FOR"]; //functionReturnValue = null
if (string.IsNullOrEmpty(functionReturnValue))
{
functionReturnValue = HttpContext.Current.Request.Headers["X-Forwarded-For"];//functionReturnValue = null
if (string.IsNullOrEmpty(functionReturnValue))
{
//If not using a proxy then get the device IP
// GetIP = Context.Request.ServerVariables("REMOTE_ADDR")
if (string.IsNullOrEmpty(functionReturnValue))
{
//If not using a proxy then get the device IP
functionReturnValue = HttpContext.Current.Request.Headers["X-CLIENT-IP"];//functionReturnValue = null
if (string.IsNullOrEmpty(functionReturnValue))
{
//If not using a proxy then get the device IP
functionReturnValue = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];//functionReturnValue = "::1"
}
}
}
}
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("(\\d{1,3}\\.){3}\\d{0,3}");
if (functionReturnValue != null)
{
if (regex.IsMatch(functionReturnValue))
{
functionReturnValue = regex.Match(functionReturnValue).Value.ToString();
}
else
{
functionReturnValue = "";
}
regex = null;
}
if (functionReturnValue == null)
{
return "";
}
else
{
return functionReturnValue;
}
}
In X-Forwarded-For you will get client ip,proxy1 & proxy2. By taking first item you will get client/user ip.
HttpContext.Current.Request.Headers["X-Forwarded-For"].Split(new char[] { ',' }).FirstOrDefault()
Hope it helps!
I have a php file on my local server which looks like this: (The database variables are in the config.php and are correct!)
<?php
require_once "config.php";
try
{
$con = new PDO("mysql:host=".$db_host.";dbname=".$db_name,$db_user,$db_password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo "Error: ".$e->getMessage();
exit();
}
if (empty($_POST["number"]) || !isset($_POST["number"]))
{
echo "Error: no number!";
exit();
}
else if (empty($_POST["password"]) || !isset($_POST["password"]))
{
echo "Error: no password!";
exit();
}
else
{
$number = $_POST["number"];
$password = md5($_POST["password"]);
$salt = sha1($_POST["password"]);
}
if (!empty($_POST["login"]))
{
$sql = $con->prepare("SELECT COUNT(`ID`) FROM ".$db_table_login_students." WHERE `number` = ? AND `password` = ? AND `salt` = ? AND user_deleted=0");
$sql->bindParam(1, $number);
$sql->bindParam(2, $password);
$sql->bindParam(3, $salt);
$sql->execute();
if($sql->fetchColumn() > 0)
{
$login = array('Login' => 'Yes');
echo json_encode($login);
$_sql = $con->prepare("UPDATE ".$db_table_login_students." SET last_login=NOW() WHERE number = ?");
$_sql->bindParam(1, $matrikelnummer);
$_sql->execute();
exit();
}
else
{
$login = array('Login' => 'No');
echo json_encode($login);
exit();
}
}
?>
And now I want to make a HTTP Post Request with Windows Phone in C# and it looks now like this:
void PostJsonRequestWebClient()
{
WebClient webclient = new WebClient();
Uri uristring = null;
uristring = new Uri("http://localhost/login.php?");
webclient.Headers["ContentType"] = "application/x-www-form-urlencoded";
string WebUrlRegistration = "";
string JsonStringParams = "login=yes&number=4340490&password=test";
webclient.UploadStringCompleted += wc_UploadStringCompleted;
webclient.UploadStringAsync(uristring, "POST", JsonStringParams);
}
private void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
try
{
if (e.Result != null)
{
string response = e.Result.ToString();
textblock1.Text = response;
}
}
catch
{
}
}
The login data with number and password are correct in my database. When I make a Get request instead of a Post I became as answer from the php script "{Login:Yes}". But when I make a Post request like the one above I became "Error: no number!". So I think the Post query string is false, but I find nothing. Can anybody help me?
Maybe, the HTTP Header should be Content-Type instead of ContentType.
Below if the code I've been using for some time to get the connection status of the device that my store app in running on. What appears to have happened recently is, whilst it still finds the correct connection profile, it returns the level as Local, rather than Internet access.
IReadOnlyList<ConnectionProfile> p = NetworkInformation.GetConnectionProfiles();
foreach (ConnectionProfile prof in p)
{
NetworkConnectivityLevel lev = prof.GetNetworkConnectivityLevel();
if (lev == NetworkConnectivityLevel.InternetAccess)
{
return true;
}
}
return false;
Can anyone tell me why this might be, and how I can persuade it that I do, in fact, have a working internet connection (which I can prove by being able to post this question :-) )?
try this one
private bool roaming;
private string connectionProfileInfo;
private async Task<bool> IsConnectedToInternet()
{
HttpWebRequest webReq;
HttpWebResponse resp = null;
// HttpStatusCode respcode;
Uri url = null;
url = new Uri("http://www.dartinnovations.com");
webReq = (HttpWebRequest)WebRequest.Create(url);
try
{
resp = (HttpWebResponse)await webReq.GetResponseAsync();
// Debug.WriteLine(resp.StatusCode);
webReq.Abort();
webReq = null;
url = null;
resp = null;
return true;
}
catch
{
webReq.Abort();
webReq = null;
return false;
}
}
private async Task<bool> CheckForConnection()
{
bool isConnected = await IsConnectedToInternet();
Debug.WriteLine(isConnected);
ConnectionProfile internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (isConnected)
{
if (internetConnectionProfile != null)//Gets metereing info, Connectionprofile gives false positives when used to check for internet connectivity
{
Debug.WriteLine("internet available");
GetMeteringInformation(internetConnCectionProfile);
}
else
{
connectionProfileInfo = "Roaming information not available";
roaming = false;
// Debug.WriteLine("no connections");
}
return true;
}
return false;
}
private async Task GetMeteringInformation(ConnectionProfile connectionProfile)
{
ConnectionCost connectionCost = connectionProfile.GetConnectionCost();
roaming = connectionCost.Roaming;
connectionProfileInfo = "Over Data Limit :" + connectionCost.OverDataLimit + " | Approaching Data Limit :" +
connectionCost.ApproachingDataLimit;
}
how to get host domain from a string URL?
GetDomain has 1 input "URL", 1 Output "Domain"
Example1
INPUT: http://support.domain.com/default.aspx?id=12345
OUTPUT: support.domain.com
Example2
INPUT: http://www.domain.com/default.aspx?id=12345
OUTPUT: www.domain.com
Example3
INPUT: http://localhost/default.aspx?id=12345
OUTPUT: localhost
You can use Request object or Uri object to get host of url.
Using Request.Url
string host = Request.Url.Host;
Using Uri
Uri myUri = new Uri("http://www.contoso.com:8080/");
string host = myUri.Host; // host is "www.contoso.com"
Try like this;
Uri.GetLeftPart( UriPartial.Authority )
Defines the parts of a URI for the Uri.GetLeftPart method.
http://www.contoso.com/index.htm?date=today --> http://www.contoso.com
http://www.contoso.com/index.htm#main --> http://www.contoso.com
nntp://news.contoso.com/123456#contoso.com --> nntp://news.contoso.com
file://server/filename.ext --> file://server
Uri uriAddress = new Uri("http://www.contoso.com/index.htm#search");
Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Authority));
Demo
Use Uri class and use Host property
Uri url = new Uri(#"http://support.domain.com/default.aspx?id=12345");
Console.WriteLine(url.Host);
try following statement
Uri myuri = new Uri(System.Web.HttpContext.Current.Request.Url.AbsoluteUri);
string pathQuery = myuri.PathAndQuery;
string hostName = myuri.ToString().Replace(pathQuery , "");
Example1
Input : http://localhost:4366/Default.aspx?id=notlogin
Ouput : http://localhost:4366
Example2
Input : http://support.domain.com/default.aspx?id=12345
Output: support.domain.com
The best way, and the right way to do it is using Uri.Authority field
Load and use Uri like so :
Uri NewUri;
if (Uri.TryCreate([string with your Url], UriKind.Absolute, out NewUri))
{
Console.Writeline(NewUri.Authority);
}
Input : http://support.domain.com/default.aspx?id=12345
Output : support.domain.com
Input : http://www.domain.com/default.aspx?id=12345
output : www.domain.com
Input : http://localhost/default.aspx?id=12345
Output : localhost
If you want to manipulate Url, using Uri object is the good way to do it.
https://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx
var url = Regex.Match(url, #"(http:|https:)\/\/(.*?)\/");
INPUT = "https://stackoverflow.com/questions/";
OUTPUT = "https://stackoverflow.com/";
Try this
Console.WriteLine(GetDomain.GetDomainFromUrl("http://support.domain.com/default.aspx?id=12345"));
It will output support.domain.com
Or try
Uri.GetLeftPart( UriPartial.Authority )
You should construct your string as URI object and Authority property returns what you need.
public static string DownloadImage(string URL, string MetaIcon,string folder,string name)
{
try
{
WebClient oClient = new WebClient();
string LocalState = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
string storesIcons = Directory.CreateDirectory(LocalState + folder).ToString();
string path = Path.Combine(storesIcons, name + ".png");
//si la imagen no es valida ej "/icon.png"
if (!TextBoxEvent.IsValidURL(MetaIcon))
{
Uri uri = new Uri(URL);
string DownloadImage = "https://" + uri.Host + MetaIcon;
oClient.DownloadFile(new Uri(DownloadImage), path);
}
//si la imagen tiene todo ej https://www.mercadolibre.com/icon.png
else
{
oClient.DownloadFile(new Uri(MetaIcon), path);
}
return path;
}
catch (Exception ex)
{
return ex.ToString();
}
}
Here's a solution that will work for all kinds of URLs.
public string GetDomainFromUrl(string url)
{
url = url.Replace("https://", "").Replace("http://", "").Replace("www.", ""); //Remove the prefix
string[] fragments = url.Split('/');
return fragments[0];
}
it will take only domain name (www.bla.com -> bla)
no Uri required
static string GetDomainNameOnly(string s)
{
string domainOnly = "";
if (!string.IsNullOrEmpty(s))
{
if (s.Contains("."))
{
string domain = s.Substring(s.LastIndexOf('.', s.LastIndexOf('.') - 1) + 1);
string countryDomain = s.Substring(s.LastIndexOf('.'));
domainOnly = domain.Replace(countryDomain, "");
}
else
domainOnly = s;
}
return domainOnly;
}
WWW is an alias, so you don't need it if you want a domain.
Here is my litllte function to get the real domain from a string
private string GetDomain(string url)
{
string[] split = url.Split('.');
if (split.Length > 2)
return split[split.Length - 2] + "." + split[split.Length - 1];
else
return url;
}
I don't like to post such a general question, but I am not seeing a lot on the topic, so I was wondering if anyone has done something like this, and whether or not this is a good implementation to go with.
EDIT Added whole method
Here is the code
protected void gridViewAttachments_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
{
//if (e.DataColumn.FieldName == "AttachmentName" && e.CellValue.ToString().ToLower().Contains("://"))
// attachmentUrl = e.CellValue.ToString();
//if (e.DataColumn.FieldName == "AttachmentName" && !e.CellValue.ToString().ToLower().Contains("://"))
// attachmentUrl = "http://" + e.CellValue;
Uri targetUri;
if (Uri.TryCreate("http://" + e.CellValue, UriKind.RelativeOrAbsolute, out targetUri))
{
attachmentUrl = new Uri("http://" + e.CellValue);
}
if (e.DataColumn is DevExpress.Web.ASPxGridView.GridViewDataHyperLinkColumn)
{
if (attachmentUrl.ToString() == "")
{
DevExpress.Web.ASPxEditors.Internal.HyperLinkDisplayControl hyperlink =
(e.Cell.Controls[0] as DevExpress.Web.ASPxEditors.Internal.HyperLinkDisplayControl);
hyperlink.Target = "_blank";
hyperlink.NavigateUrl = ApplicationUrl + "/Attachment.ashx?key=" + hyperlink.Text;
hyperlink.Text = GetWords("GENERAL.VIEW_ATTACHMENT");
}
else
{
DevExpress.Web.ASPxEditors.Internal.HyperLinkDisplayControl hyperlink = (e.Cell.Controls[0] as DevExpress.Web.ASPxEditors.Internal.HyperLinkDisplayControl);
hyperlink.Target = "_blank";
hyperlink.NavigateUrl = attachmentUrl.ToString();
hyperlink.Text = "Go to URL";
}
}
}
Pretty basic, and it works. My question is this: Is checking if the string contains :// enough to check whether or not it is a url? The reason I have to check is it is pulling the data from table and some of the fields in the table are filenames (mydoc.docx) in which case I will do something else with them. Is there another more robust check I can do in C#?
You could use Uri.TryCreate instead to see if creation of the URL is successful:
Uri targetUri;
if (Uri.TryCreate("http://" + e.CellValue, UriKind.RelativeOrAbsolute, out targetUri))
{
//success
attachmentUrl = "http://" + e.CellValue;
}