Web Request is a type and cannot be used as an expression - vb.net

I am making an app for Windows Phone and since I need some login cookies that are saved in Web browser control, I am using the code from here: Grabbing Cookies in Web Browser Control - WP7 to create a HTTPWebRequest and get some JSON data for parsing but I am getting two errors on the last line of this snippet
Dim browser = New WebBrowser()
Dim brwhttp = GetType(WebRequestCreator).GetProperty("BrowserHttp")
Dim requestFactory = TryCast(brwhttp.GetValue(Browser, Nothing), IWebRequestCreate)
Dim uri = New Uri("some-url")
Dim req = requestFactory.Create(uri)
req.Method = "GET"
req.BeginGetResponse(New AsyncCallback(request_CallBack), WebRequest)
(1). 'WebRequest' is a type and cannot be used as an expression.
(2). Delegate 'System.AsyncCallback' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.
How to fix?
UPDATE 1: Fixed Error (2) by adding 'Address Of' before 'request_CallBack' but Error (1) still remains.
UPDATE 2: Fixed all errors but not getting any response (empty). What's wrong with my code?

THe problems are two do with the line req.BeginGetResponse(New AsyncCallback(request_CallBack), WebRequest) This has two problems.
VB.Net does not have true first class functions so you have to use the AddressOf operator the create a delegate. Also the compiler will infer the delegate type so you don't need the constructor
BeginGetResponse takes a state object as the second parameter not a type expression. This is whatever you need in the callback
so:
req.BeginGetResponse(AddressOf request_CallBack, Nothing)
If you need to pass a type to the request callback use
req.BeginGetResponse(AddressOf request_CallBack, GetType(WebRequest))
However this looks like a copying change and what you want may be
req.BeginGetResponse(AddressOf request_CallBack, req)

Related

Format this GET request in VB.NET code behind?

I am trying a "GET" method to request a count of activities from the Accelo API here:
https://api.accelo.com/docs/?http#count-activities
And although i've used a very similar POST method to successfully get the access token using an authentication method, I cannot for the life of me figure out how to get the count of activities. The API says to use "GET" and past the access token as "bearer..." and I've also tried doing a post and getting the stream first, tried sending in some data and accessing the "list activities" endpoint instead...nothing is working. everything I do returns the error "400. Bad Request."
I've tried passing data in a query string format directly in the URI, and tried passing no data since its a GET function. It looks to me like I'm following the API exactly.
Dim data2 = Nothing ' Encoding.UTF8.GetBytes(jsonstring)
Dim _list = GetListOfActivities(New Uri("https://example.api.accelo.com/api/v0/activities/count.xml"), data2, _AccessToken)
Dim reqa As WebRequest = WebRequest.Create(uri)
' reqa.Method = "GET"
reqa.Headers.Add("GET", "/api/v0/activities/count.xml HTTP/1.1")
reqa.Headers.Add("Authorization", "Bearer " & _AccessToken)
reqa.ContentType = "application/x-www-form-urlencoded"
'reqa.ContentLength = jsonDataBytes.Length
' Dim streama = reqa.GetRequestStream()
' streama.Write(jsonDataBytes, 0, jsonDataBytes.Length)
'streama.Close()
Dim responsea As WebResponse = reqa.GetResponse()
Console.WriteLine((CType(responsea, HttpWebResponse)).StatusDescription)
I must be formatting the request wrong - please help?
My problem turned out to be something stupid, and literally beyond the scope of what I posted here. I had specified a "scope" in my initial request to get the access token that was set to read-only "staff" data (I had copied-and-pasted their example online into my code, for other parameters like grant type, and I brought the scope along with it), and in this scenario here I was trying to access "activities" data and not "staff" data. I would have thought I'd get a permissions-related error, instead of "bad request" which confused me, but anyway it works now.
The above code - actually with this line:
reqa.Method = "GET"
instead of this line:
reqa.Headers.Add("GET", "/api/v0/activities/count.xml HTTP/1.1")
Works just fine since I changed my scope to read(all) in my initial web method getting the access token.

Invalid Session ID from SalesForce REST API

I'm making a call to the SalesForce REST Api.
However, I have a very weird error. Initially I wrote this in a VB.NET Module file. Everything was working correctly, as soon as I put the method in a class I keep getting the darn Invalid Session ID error. The error doesn't seem to suggest which part of the code is failing.
I am using RestSharp to facilitate the call to the SalesForce API, and I know for a fact it works because in the Module file I am able to fetch the data that I need.
I do not understand the rationale about why it works in the Module file but not in a class file.
Anyways, here is the code that I've placed in both the class file and the Module file to facilitate the call:
client = New RestClient(_strURI)
request = New RestRequest(Method.POST)
request.AddParameter("grant_type", "password")
request.AddParameter("client_id", _strClientID)
request.AddParameter("client_secret", _strClientSecret)
request.AddParameter("redirect_uri", _strRedirectUrl)
request.AddParameter("username", _strUserName)
request.AddParameter("password", _strPassword & _strSecurityToken)
'Dim response As IRestResponse
Dim response As IRestResponse = Nothing
response = client.Execute(request)
'This is the offending line in the class file..it returns back a HTTP 200 yet gives me that Session Invalid error
If response.StatusCode <> HttpStatusCode.BadRequest Then
token = JsonConvert.DeserializeObject(Of TokenResponse)(response.Content)
Dim listViewResults As String = HttpGet(token.instance_url & "/services/data/v38.0/sobjects/Contact/listviews", "")
I am unsure whether this is a permission issue or more of a programming error; because in both cases all of the authentication strings are the same. At the moment I've hardcoded it to point to a test profile. So I can't pinpoint what exactly I am doing wrong?
Any help would suffice.
Stupid me,
Anyways shortly after I posted this I went and took a brisk walk outside of the office. Turned out to be a misnomer.
I had the same HttpGet method defined in the module file and forgot to copy and paste that into the class file. When I actually stepped through it, it threw an Exception inside.
Turned out the only fix needed was to put that method into the class file.

What Sanitizes Data From REST/Web APIs?

I have a REST Service built in vb.net running on IIS. I've noticed that the returned data is sanitized or escaped, e.g.:
<script>alert('testing1')</script>
is returned as:
<script>alert('testing1')</script>
Can anyone tell me what is doing the escaping, would it be .net or IIS and can it be switched on or off?
I'm pretty sure you could have a controller method like this..
Function ReturnJavascript() As Net.Http.HttpResponseMessage
Dim resp As New Net.Http.HttpResponseMessage(Net.HttpStatusCode.OK)
resp.Content = New Net.Http.StringContent("<script>alert('testing1')</script>", System.Text.Encoding.UTF8, "text/plain")
Return resp
End Function
Not sure what you are trying to accomplish. If you don't want to return as a response message, you might attempt returning as content set to text/plain.

SSRS ReportViewer fails when using parameters

I'm using the ReportViewer control in VS2010.
In my local and development environments when I use:
ReportParameter[] toReportParameterList = new ReportParameter[1];
toReportParameterList[0] = new ReportParameter("intApplicationID", Session["APPLICATION_ID"]);
rvFinancialSummaryReport.ServerReport.SetParameters(toReportParameterList);
Everything works properly.
When I try to do the exact same thing in my Staging/Beta environment, I get an HTTP 401 not authorized error.
When I comment out those three lines, the report parameter section loads and I can get the report correctly when I type in the parameter.
Any idea why trying to pass parameters via the server report, I get an HTTP 401?
EDIT
I should probably add that I am sending credential information as such:
string tsUserNameDomain = ConfigurationManager.AppSettings["ReportUserName"].ToString();
string tsDomain = tsUserNameDomain.Substring(0, tsUserNameDomain.IndexOf("\\"));
string tsUsername = tsUserNameDomain.Substring(tsUserNameDomain.IndexOf("\\") + 1);
string tsPassword = ConfigurationManager.AppSettings["ReportPassword"].ToString();
ReportViewerCredentials toCredentials = new ReportViewerCredentials(tsUsername, tsPassword, tsDomain);
rvFinancialSummaryReport.ServerReport.ReportServerCredentials = toCredentials;
This works as long as there is no parameters set.
EDIT
Just to Reiterate... when I comment this line:
rvFinancialSummaryReport.ServerReport.SetParameters(toReportParameterList);
It works...
When I uncomment this line:
rvFinancialSummaryReport.ServerReport.SetParameters(toReportParameterList);
I get an HTTP 401: Unauthorized.
EDIT
Another detail... my SSRS server is HTTP, my IIS is HTTPS...
It would seem that the order of operations in this scenario is important.
After some extensive testing on a local server, I realized that after the parameter value was set, when I went to set the credential values the parameter array object on the server report was coming back as null.
I moved the instantiation of the credential object to the top of the page load, and now everything is working properly.

Passing RequestHeader Play2.1

I have a Problem in compiling play 2.1 application ,in main.scala.html I called javascriptRouters
javascriptRouter("jsRoutes")(
routes.javascript.Authentication.authenticate
For this I have defined (request: play.api.mvc.RequestHeader) on top level
so its working fine now I got request object in this page but when calling
#main("title")
Unspecified value parameter content.
[error] #main(title = "Create Job",status,role){
how to pass request object ????
`
I solved my problem by passing it in the new parenthesis but have a new questions
why it is not working when passed it with content
The first one I understand that whenever I am calling #main I have to passs three string
what does second parenthesis mean (content :Html) are we passing the caller html here
that what does this mean?
#(title:String, status:Option[String],role:String)(content: Html)(implicit request: play.api.mvc.RequestHeader)