Visual Basic - Web Request 403 Forbidden - vb.net

I am new to Visual Basic so I dont know much about Syntax e.t.c
So if anyone could help me out that be great!
Im getting a 403 Forbidden Error most likely because of the User Agent being used. The website is probably picking up that its not an actual user entering the Website and therefore blocks access.
How do I apply a User Agent that will bypass this issue? I have no idea where to put the Syntax.
My Code:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sources(1) As String
array(0) = "http://proxy-ip-list.com"
array(1) = "http://sslproxies24.blogspot.com/feeds/posts/default"
For Each element As String In sources
'Connect to the Proxy Source;
Dim source As Net.HttpWebRequest = Net.WebRequest.Create(element)
'Prepare the Response;
Dim response As Net.HttpWebResponse = source.GetResponse
'Load the HTML;
Dim reader As IO.StreamReader = New IO.StreamReader(response.GetResponseStream())
Dim html As String = reader.ReadToEnd
'Regex;
Dim expression As New Regex("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,4}")
'Set the Matches variable to the Matched Sections of the HTML source;
Dim matches As MatchCollection = expression.Matches(html)
'Add the proxies to the ListBox;
For Each proxy As Match In matches
ListBox1.Items.Add(proxy)
Next
Next
End Sub
End Class

Related

Sharepoint connection fails (Winforms, VB.Net)

I did a small development where it is necessary to connect to a SharePoint list, the development works well with my user, but when testing with another user who has the same privileges on the site, it sends the following error message
"The sign-in name or password does not match one in the Microsoft Account System. "
I already tried with other users that the site are owners, but I get the same result.
Can you help me see what is the error in the above?
This is my code.
Imports Microsoft.SharePoint.Client
Imports Microsoft.SharePoint
Imports System.Security
Imports System.Net
Public Class Form4
Dim siteUrl As String = "https://example.sharepoint.com/site/SiteExample/"
Dim context As New ClientContext(siteUrl)
Dim web As Web
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim userNameSP As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim secureString As SecureString = New NetworkCredential("", password).SecurePassword
Dim cred = New SharePointOnlineCredentials(userNameSP, secureString)
Dim clientContext As New ClientContext(siteUrl)
clientContext.Credentials = cred
Dim web As Web = clientContext.Web
Dim oWebsite As Web = clientContext.Web
Dim collList As ListCollection = oWebsite.Lists
Dim oList As List = collList.GetByTitle("Example Test")
clientContext.Load(oList)
clientContext.ExecuteQuery()
Dim query As CamlQuery = CamlQuery.CreateAllItemsQuery()
query.ViewXml = "<View Scope='RecursiveAll'><Query><ViewFields><FieldRef Name='Category/></ViewFields></Query></View>"
Dim AllItems As ListItemCollection = oList.GetItems(query)
clientContext.Load(AllItems)
clientContext.ExecuteQuery()
If AllItems.Count > 0 Then
... Do Something
end if
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Thank you in advance.
Did the user account enable MFA Authentication ?
I tested the same code snippet with a normal user account without MFA, it's working as expected.
Please check MFA Status in this:
https://account.activedirectory.windowsazure.com/UserManagement/MultifactorVerification.aspx?BrandContextID=O365
If enabling the MFA, please disable it, as CSOM didn't support MFA currently.
This is the solution code to do log-in in a sharepoint.
Imports Microsoft.Office.Interop
Imports System.IO
Imports Microsoft.SharePoint.Client
Imports Microsoft.SharePoint
Imports System.Security
Imports System.Net
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim siteUrl As String = "https://example.sharepoint.com/sites/TRAINING/"
Dim authManager = New OfficeDevPnP.Core.AuthenticationManager()
Dim ctx As ClientContext = authManager.GetWebLoginClientContext(siteUrl)
Dim web As Web = ctx.Web
ctx.Load(web, Function(w) w.Title)
ctx.ExecuteQuery()
Console.WriteLine("You have connected to {0} site, with Multi Factor Authentication enabled!!", web.Title)
Try
Do Something...
Catch ex As Exception
MessageBox.Show("An error occurred:" & Environment.NewLine & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Sorry for the delay.
Regards.

Visual basic HTTP request

Assume, that I wish to use an API from abc.com website.In the api documentation it says,
HTTP/1.x 200 OK
GET /api/now/news
Host: abc.com
Api-Key:xxxxxxxxxx
to get news.
How to write the codes to get a successful response in VB.net
Thank you.
Take https://www.google.com/ for an example, get the source code of the web page in UTF-8 format.
Imports System.Net
Imports System.Text
Public Class Form1
Public Function Getwebcode(ByVal url As String, ByVal encoder As String) As String
Dim myWebClient As WebClient = New WebClient()
Dim myDataBuffer As Byte() = myWebClient.DownloadData(url)
Dim SourceCode As String = Encoding.GetEncoding(encoder).GetString(myDataBuffer)
Return SourceCode
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim back As String = Getwebcode("https://www.google.com/", "UTF-8")
TextBox1.Text = back
End Sub
End Class

vb.net XmlDocument.SelectSingleNode and xmlNamespace issue

I am trying to get this code working but somehow is not getting the content
My Code
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
m_xmld.Load("http://feeds.thescore.com/nfl.rss")
'Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/rss/channel/item")
Dim content As XmlNamespaceManager = New XmlNamespaceManager(m_xmld.NameTable)
content.AddNamespace("content", "http://schemas.xmlsoap.org/soap/envelope/")
'Loop through the nodes
For Each m_node In m_nodelist
If Not m_node.SelectSingleNode("content:encoded",content) Is Nothing AndAlso Len(m_node.SelectSingleNode("content:encoded",content).InnerText) > 0 Then
response.Write(m_node.SelectSingleNode("content:encoded",content))
response.Flush()
end if
Next
Can someone help me please?
I did it this way and could see all of the content nodes, that appear to be html.
Imports <xmlns:atom="http://www.w3.org/2005/Atom">
Imports <xmlns:content="http://purl.org/rss/1.0/modules/content/">
Imports <xmlns:dc="http://purl.org/dc/elements/1.1/">
Imports <xmlns:georss="http://www.georss.org/georss">
Imports <xmlns:media="http://search.yahoo.com/mrss/">
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xe As XElement = XElement.Load("http://feeds.thescore.com/nfl.rss")
Dim ie As IEnumerable(Of XElement) = xe...<content:encoded>
For Each c As XElement In ie
Debug.WriteLine(c.Value)
Next
End Sub
End Class
I managed with different approach I import first the data with webClient replaced the with and worked like a charm

to save xml file in Vb.net

Public Function UploadCourseXML(ByVal fileStream As String, companyID As Integer, ByVal tokenID As String) As String Implements ICorePointService.UploadCourseXML
If (Not IsCustomerAuthentication(companyID, tokenID)) Then
Throw New Exception("Authentication failed. Please provider Company ID and Token ID")
End If
Dim doc As XDocument = XDocument.Parse(fileStream)
doc.Save("Update_XML")' error occures here... Access to the path c:\...etc. is denied
.. i want to save this in solution explorer
Return "result"
End Function
Refer this link :
http://support.microsoft.com/kb/301233
This for windows & web also
Imports System.IO
Imports System.Net
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'To just download xml text
'Download text and save
Dim wc As New WebClient
Dim xmlText As String = wc.DownloadString("http://www.localcallingguide.com/xmllocalexch.php?exch=015800")
File.WriteAllText("new file path.xml", xmlText)
'Or
'To load stream directly into XML Document :
'Get data in stream
Dim webRequest As WebRequest = webRequest.Create("http://www.localcallingguide.com/xmllocalexch.php?exch=015800")
Dim webResponse As WebResponse = webRequest.GetResponse
Dim webStream As Stream = webResponse.GetResponseStream
'Optionally
'If you want you can read text from stream
'Dim reader As New StreamReader(webStream)
'reader.ReadToEnd 'will give same output as wc.downloadString()
'Load stream
Dim xmlDoc As New XmlDocument
xmlDoc.Load(webStream)
'select any level nodes using xpath
Dim Nodes As XmlNodeList = xmlDoc.SelectNodes("//lca-data/prefix/exch")
'iterate in selected nodes
For Each node As XmlNode In Nodes
RichTextBox1.AppendText(node.InnerText & vbCrLf)
Next
End Sub
End Class
You'll get a Access to the path '...' is denied error if the folder is flagged ReadOnly. You need to remove this flag prior to saving the file.
Dim info As DirectoryInfo = New DirectoryInfo("C:\folder1\folder2\folder3")
If (info.Exists AndAlso ((info.Attributes And FileAttributes.[ReadOnly]) = FileAttributes.[ReadOnly])) Then
info.Attributes = (info.Attributes Xor FileAttributes.[ReadOnly])
End If
If you're writing to an existing file, also make sure the file is not flagged ReadOnly. Just change DirectoryInfo to FileInfo.
Dim info As FileInfo = New FileInfo("C:\folder1\folder2\folder3\file.ext")
You may refer to the below given link to find out your problem,
http://support.microsoft.com/kb/2623670#method4

VB.net link extraction with HtmlAgilityPack

I was able to extract URLs with simple href tags like this:
<a href="http://www.samplesite.com">
but my problem is how do i extract a link from an href tag that looks like this?
<a href="http://www.wherecreativitygoestoschool.com/vancouver/left_right/rb_test.htm" onmousedown="return rwt(this,'','','','1','AFQjCNHvlwTxfBVEYcqGUnilAZN0uY2IXw','','0CCsQFjAA','','',event)">
Right Brain vs Left Brain Creativity <em>Test</em> at The Art Institute of <b>...</b></a>
Here is my complete code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim webClient As New System.Net.WebClient
Dim WebSource As String = webClient.DownloadString("http://www.google.com.ph/search?hl=en&as_q=test&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=countryCA&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&tbs=ctr%3AcountryCA&as_filetype=&as_rights=#as_qdr=all&cr=countryCA&fp=1&hl=en&lr=&q=test&start=20&tbs=ctr:countryCA")
Dim doc = New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(WebSource)
Dim links = GetLinks(doc, "test")
For Each Link In links
ListBox1.Items.Add(Link.ToString())
Next
End Sub
Public Class Link
Public Sub New(Uri As Uri, Text As String)
Me.Uri = Uri
Me.Text = Text
End Sub
Public Property Text As String
Public Property Uri As Uri
Public Overrides Function ToString() As String
Return String.Format(If(Uri Is Nothing, "", Uri.ToString()))
End Function
End Class
Public Function GetLinks(doc As HtmlAgilityPack.HtmlDocument, linkContains As String) As List(Of Link)
Dim uri As Uri = Nothing
Dim linksOnPage = From link In doc.DocumentNode.Descendants()
Where link.Name = "a" _
AndAlso link.Attributes("href") IsNot Nothing _
Let text = link.InnerText.Trim()
Let url = link.Attributes("href").Value
Where url.IndexOf(linkContains, StringComparison.OrdinalIgnoreCase) >= 0 _
AndAlso uri.TryCreate(url, UriKind.Absolute, uri)
Dim Uris As New List(Of Link)()
For Each link In linksOnPage
Uris.Add(New Link(New Uri(link.url, UriKind.Absolute), link.text))
Next
Return Uris
End Function
I have noticed that my code does not extract links that ends with </a>. Is there anything i can do to modify my code that it would extract links ending with </a>?
Use following code to grab all of links having 'href' attribute from page:
Dim hNodeCol as HTMLNodeCollection = doc.DocumentNode.SelectNodes("//a[#href]")
...if you still need it, ofcourse ;)