New WebProxy to read from list box VB.net - vb.net

I am using http web requests and want it to use different proxies.
This is my starting point:
Dim myproxy As New WebProxy("http://1.1.1.1:80")
I would like to populate the address section using an item from ListBox.
VB does not let me do it, because I am trying to convert a string to an address data type.
Is there a way?

If you managed to solve your problem with the C# code you posted, VB could look like this:
Dim prx As String = "http://" & lstProxy.Items(x)
Dim myProxy As New WebProxy(prx)
And if you write same in C#, there is hardly any difference.

Ok - could not figure it out! But have since switched to C#.net and it's all good.
I've handled it like this:
string front = "http://";
string prx = front + lstProxy.Items[x].ToString();
WebProxy myProxy = new WebProxy(prx);
So, top advice from me is to get C#.Net. Feel free to comment on this if any help is needed.

Related

Remove parts of a string in vb.net

Im using the FolderBrowserDialog to pick a path.
It will return for ex. this= C:\Mypath1\Mypath2\DOCS
I would like to remove everything but DOCS.
If i use VBA i would use a InStrRev combined with a left. But now in in VB.net and im not sure how to achieve this, im pretty sure there is something better than my old VBA way?
Anyone there that could help, google failed me.
Try this:
IO.Path.GetFileName("C:\Mypath1\Mypath2\DOCS")
Which returns DOCS.
I am not sure what you want to do. But maybe this can help you get the last part of a string
Dim fullPath As String = " C:\Mypath1\Mypath2\DOCS"
Dim Parts As String() = fullPath.Split("\")
Dim lastPart As String = Parts(Parts.Length - 1)

VB.NET Get the V variable from a youtube url

I am writing a program to play videos from the internet in it.
for the user to get the video they have to paste a url in E.G. https://www.youtube.com/watch?v=5xniR1GN69U that url works but this one https://www.youtube.com/watch?feature=c4-overview&list=UUOYWgypDktXdb-HfZnSMK6A&v=5xniR1GN69U dosnt
this is my currunt code
how do i get it to just the v varibale from the url
Your querystring looks like "?v=5xniR1GN69U" the first time and "?feature=c4-overview&list=UUOYWgypDktXdb-HfZnSMK6A&v=5xniR1GN69U" the second time. You could have figured that out by yourself by simply debugging and checking your variables, since it doesn't even reach the code in the if block... Making a screenshot doesn't help people to figure out what's wrong with your code.
You might want to use Regex to parse the id Regex.Match(str, "v=(\w+)").Groups(1).Value
Just request the parameter v?
Request("v")
or
Request.Querystring("v")
EDIT
If in winforms. You can apply as suggested by #Markus
Try it like this
Dim querystring As String = url.Query
Dim myMatches As MatchCollection
Dim MyRegEx As New Regex("v=+(\w+)")
myMatches = MyRegEx.Matches(querystring)
For Each Row In myMatches
AxShockFlashMovie1.Movie = "https://www.youtube.com/v/" & Row.ToString() & ""
Next
You will also need to include
Imports System.Text.RegularExpressions
I've used HttpUtility.ParseQueryString in the past for something similar. Don't forget that youtube also offer tiny version of the urls with http://youtu.be/???

Check if cookie exists #2

Trying to start with VB.Net and having problem
We have such a Class in C#.
How to check if cookie exist? Exactly, how to set input object "Cookie" for a function CookieExist? How to create it using GeckoFx?
For example, how to find cookie with the name "Test2"?
Please, make an example.
I'm going crazy with VB.Net
P.S. I rarely write to forums if can find info on my own but VB.Net have too few examples in the net. ;/
I think the code below will help you:
Dim cookieValue As String = "Value of cookie"
Dim cookieName As String = "CookieName"
Dim realCookie As HttpCookie = HttpContext.Current.Request.Cookies.Get(cookieName)
If IsNothing(realCookie) Then
HttpContext.Current.Request.Cookies.Add(New HttpCookie(cookieName, cookieValue))
End If

How to set user "logon to" AD attribute in VB.NET

I'm working on upgrading a solution in VB.NET that is heavily based on Active Directory. As of now, I'm trying to add a PC restriction to a new AD User upon user creation. Essentially, I need to update the Logon To attribute to include 1 or more PCs, how do I go about doing this?
I learned that I am interested in the IADsUser property "LoginWorkstations" (thanks to http://msdn.microsoft.com/en-us/library/Aa746340). As of now, I have code that can fetch this attribute from any AD user, but I cannot set it.
Here is the code I have to fetch the attribute:
Dim userADObject As new DirectoryEntry(ADPath)
Dim logonToPC as String = userADObject.InvokeGet("LoginWorkstations")(0).ToString
That will fetch the first restricted PC (if there is one) and save it in logonToPC and will look something like "PC10000"
That works great, so intuitively I would assume something like this would work:
Dim userADObject As new DirectoryEntry(ADPath)
Dim args() As Object = {"PC100001"}
userADObject.InvokeSet("LoginWorkstations", args)
But it doesn't work... It just throws a rather unhelpful exception.
I've tried testing this approach with a different attribute and it works just fine. Not much out there on Google either unfortunately...
Any help would be greatly appreciated.
You should be able to do this fairly easily - also: note that you should use the userWorkstations LDAP attribute (see note here) - this is multi-valued, e.g. it allows multiple entries.
Dim userADObject As new DirectoryEntry(ADPath)
userADObject.Properties("userWorkstations").Add("PC001")
userADObject.Properties("userWorkstations").Add("PC002")
userADObject.Properties("userWorkstations").Add("PC003")
userADObject.CommitChanges()
If you have the necessary permissions to update Active Directory, that should basically do it, I think.
Found the solution that works. I took marc_s's code and modified a bit to work properly. here's what I have:
Dim userADObject As New DirectoryEntry(Me.ADPath)
'Grab the previous restriction, because we may have to clear it first in the future
Dim priorRestriction As String = userADObject.Properties("userWorkstations").Value
If priorRestriction = "" Then
'Simply add
userADObject.Properties("userWorkstations").Add("PC001,PC002")
Else
'Important - We have to clear the old restriction before adding the new
userADObject.Properties("userWorkstations").Remove(priorRestriction)
'Now add the new restriction
userADObject.Properties("userWorkstations").Add(priorRestriction & ",PC003")
End If
'Commit!
userADObject.CommitChanges()
Something that gave me some pretty good grief what that I you can't have a space in the string being added. Example: .Add("PC001, PC002") has to be .Add("PC001,PC002")

Get "real" IP address with VB.NET?

I'm looking for a function that will give me my real IP, not my local IP. the function i currently have, returns the ip in network and sharing center which is 192.168.2.100
But if I go to whatismyip, then it gives my real IP.
How could I get this using VB.NET?
thanks
To Combine the answers above"
Create a php file and paste this in it:
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
save it as curip.php and upload it to your server.
In your VB.net project create a module.
Declare the imports section at the very top
Imports System.Net
Imports System.IO
And create your function:
Public Function GetIP() As String
Dim uri_val As New Uri("http://yourdomain.com/curip.php")
Dim request As HttpWebRequest = HttpWebRequest.Create(uri_val)
request.Method = WebRequestMethods.Http.Get
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim myIP As String = reader.ReadToEnd()
response.Close()
Return myIP
End Function
Now anywhere in your code you can issue
Dim myIP = GetIP()
as use the value from there as you wish.
There's no way to do this just with VB.Net. You need to find a website that will tell you (perhaps one of your own?) or you need to interface with your router.
If you have a web site that is capable of running any sort of web page applications you can create a web page that simply outputs the client's (as in the computer connecting to the page) IP address.
I have one of my own:
http://etoys.netortech.com/devpages/ip.asp
Though I can't guarantee it will always be there which is why you should make your own.
From there it's a simple matter of using a HttpWebRequest to download the content of the page.
I'm probably just being crotchety here, but I can't help but think that your "real" IP address is the one returned by ifconfig (ipconfig) on your local machine. 192.168.2.100 in your case. Whatever NAT translation or tunneling goes on between you and the remote endpoint shouldn't matter, and if it does, there's a good chance you're doing something that could make your application only work in your current environment.
I would use a publicly available site which returns your public IP address in response.
The key factors here are:
Availability of the service. Running your own service guarantees full control over it and knowledge of when it is available and when it's not. But in some cases it might be just too much work for such a simple task.
Minimizing additional clutter contained in the response. There are plenty sites allowing you to get your public IP address, but they often do that in the form of a HTML page. Extracting the small fragment of the page containing the IP address might need additional code.
Keeping in mind these two factors I would recommend this URL: http://wtfismyip.com/text It has the advantage of returning only the IP address in textual form. There are also versions for:
JSON: http://wtfismyip.com/json
XML: http://wtfismyip.com/xml
Pick the format that is easier for you to parse.
You DON'T need to use a php file just use a site that shows your ip like ip-adress.com and then get the ip from there with webrequest and then use GetBetween function.
have fun :)
Using PHP it can be done simply:
As "shaiss" shared the PHP code, this is the VB.net code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myip As HttpWebRequest = HttpWebRequest.Create("http://YourHosting.com/curip.php")
Dim grab As HttpWebResponse = myip.GetResponse()
Dim stream As Stream = grab.GetResponseStream
Dim SR As New StreamReader(stream)
TextBox1.Text = SR.ReadToEnd()
End Sub
End Class
This is just an example with:
1 - Form
1 - TextBox
1 - Button(s)
Hope this helps!
I had the same question and searched a bit only to remember that I can do this with WebBrowser!
Private Sub getExtIP() Handles activeProjectsWB.DocumentCompleted
If gotIP = False And populateProjectCollectionBLN = True Then
If activeProjectsWB.ReadyState = WebBrowserReadyState.Interactive Or activeProjectsWB.ReadyState = WebBrowserReadyState.Complete Then
Dim unformattedExtIP As String = activeProjectsWB.Document.GetElementsByTagName("title").Item(0).OuterHtml
Dim onlyIPAddress As String = String.Empty
For Each character In unformattedExtIP
Dim result As Integer = 0
If Not (Integer.TryParse(character, result) = 0) Or character = "." Then
onlyIPAddress = onlyIPAddress & character
End If
Next
extIP = onlyIPAddress
gotIP = True
End If
End If
End Sub
This subroutine only is triggered when you navigate to a webpage using WebBrowser.Navigate().
The gotIP boolean exists because I have another subroutine that activates on any document completion. I don't want it to be triggered more than once. If you are unfamiliar with WebBrowser, you check to make sure the web page is loaded enough with ReadyState. If you don't, you may get an exception (because content isn't loaded).
You can use whichever site you like. This site is good because it puts your IP address in the title. That's good because there's going to be one title tag. In the event you can't use this site (or one that has a unique tag with your content within it), use a For Each loop.
For Each instance As HtmlElement In activeProjectsWB.Document.GetElementsByTagName("InsertTagHere")
'do something to find the tag that contains your IP address
Next