Windows Phone VB Web Request - vb.net

does anybody has an idea how I can perform an asynchronus Post Request in VB.Net for Windows Phone 8?
I tried a lot but nothing worked... also this http://msdn.microsoft.com/de-de/library/system.net.httpwebrequest.begingetrequeststream.aspx didn't work.
Thanks a lot.

I had to figure this out for myself a while ago. Let me see what I can do to help.
Posting a web request is actually simpler than that link shows. Here's what I do.
First, I create a MultipartFormDataContent:
Dim form as New MultipartFormDataContent()
Next, I add each string I want to send like this:
form.Add(New StringContent("String to sent"), "name of the string you are sending")
Next, create a HttpClient:
Dim httpClient as HttpClient = new HttpClient()
Next, we'll create a HttpResponseMessage and post your information to the url of your choice:
Dim response as HttpResponseMessage = Await httpClient.PostAsync("www.yoururl.com/wherever", form)
Then, I usually need the response as a string, so I read the response to a string:
Dim responseString as String = Await response.Content.ReadAsStringAsync()
This will give you the response you wanted, if that's what you wanted.
Here's an example of a method I use:
Public Async Function GetItems() As Task
Dim getUrl As String = "https://myapiurl.com/v3/get"
Dim responseText As String = String.Empty
Dim detailType As String = "complete"
Try
Dim httpClient As HttpClient = New HttpClient()
Dim form As New MultipartFormDataContent()
form.Add(New StringContent(roamingSettings.Values("ConsumerKey").ToString()), "consumer_key")
form.Add(New StringContent(roamingSettings.Values("access_token").ToString()), "access_token")
form.Add(New StringContent(detailType.ToString()), "detailType")
Dim response As HttpResponseMessage = Await httpClient.PostAsync(getUrl, form)
responseText = Await response.Content.ReadAsStringAsync()
Catch ex As Exception
End Try
End Function
If you aren't using the Http client libraries, you need to install them like this:
What you need to do to use the HttpClient, is to navigate in Visual Studio, go to Tools->Library Package Manager->Manage Nuget Packages for this solution. When there, search the online section for HttpClient and make sure you have "Include Prerelease" selected in the listbox above the results. (Default is set to "Stable Only")
Then install the package with the ID of Microsoft.Net.Http
Then you'll need to add an Import statement at the beginning of the document you are using it in.
Let me know if this is what you were looking for.
Thanks,
SonofNun

Related

CRM16 - Trigger custom action from WebApi

I've built a custom action in CRM that I need to fire through its WebAPI. The custom action is activated and I got no errors in CRM while creating it.
I try to call this action from a VB.NET application like:
Dim httpch As New HttpClientHandler
Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_addnotetocontact"
httpch.Credentials = New NetworkCredential("username", "password", "domain")
Dim httpClient As New HttpClient(httpch)
httpClient.BaseAddress = New Uri(CRMWebApiUri)
httpClient.Timeout = New TimeSpan(0, 2, 0)
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0")
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0")
httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations='OData.Community.Display.V1.FormattedValue'")
httpClient.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
Dim jsonNote As JObject = New JObject(New JProperty("NoteTitle", "'Mails have been deleted'"), New JProperty("NoteText", "This contacts SmarterMail data has been deleted due to inactivity"))
Dim postData = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
Dim retrieveContactResponse As HttpResponseMessage = httpClient.PostAsync(requestUri, postData).Result
What i get back is a status 400 with a message:
Request message has unresolved parameters.
I can make other calls to the same site and get all contacts as an example
What does this mean and how do I fix it ?
What does this mean and how do i fix it ?
Referencing Request message has unresolved parameters.
In CRM when you get this error while calling action. then there may be
three reasons behind that
some parameters you are passing wrong. (make sure action name is correctly pass)
your action is not activated
your action name is duplicate and one action is in active mode and other is in draft.(as this is done from CRM side that one has to be in
draft only two same name action wont be active at same time.)
No. 2 is already taken care of as it was already state that the custom action is activated.
No. 3 is addressed in the linked article and is plausible if you may have imported the actions twice in CRM or inadvertently created two actions with the same name.
To address no.1, I would suggest creating an object model to hold the data to be sent
Public Class Note
Public Property NoteTitle As String
Public Property NoteText As String
End Class
CRM is very finicky about proper parameter formatting. Parameter names are also case sensitive. The '' in the NoteTitle will cause issues when serializing.
Also, if possible use NewtonSoft.Json to craft the JSON payload instead of trying to build it on your own.
'Handler with credentials
Dim httpClientHandler As New HttpClientHandler With {
.Credentials = New NetworkCredential("username", "password", "domain")}
'Create and configure HTTP Client
Dim httpClient As New HttpClient(httpClientHandler) With {
.BaseAddress = New Uri(CRMWebApiUri),
.Timeout = New TimeSpan(0, 2, 0)}
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0")
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0")
httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations='OData.Community.Display.V1.FormattedValue'")
httpClient.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
'Create and populate data to be sent
Dim model As New Note With {
.NoteTitle = "Mails have been deleted",
.NoteText = "This contacts SmarterMail data has been deleted due to inactivity"}
'Serialize mode to well formed JSON
Dim json As String = JsonConvert.SerializeObject(model)
Dim postData = New StringContent(json, Encoding.UTF8, "application/json")
'invoking action using the fully qualified namespace of action message
Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_addnotetocontact"
'POST the data
Dim retrieveContactResponse As HttpResponseMessage = Await httpClient.PostAsync(requestUri, postData)
Additional reference Dynamics CRM 2016: Use Web API actions
When invoking a bound function, you must include the full name of the
function including the Microsoft.Dynamics.CRM namespace. If you do not
include the full name, you will get the following error: Status
Code:400 Request message has unresolved parameters.
Change the name new_addnotetocontact into new_AddNoteToContact, it will work.
Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_AddNoteToContact"
Process Schema is like below:
<Action Name="new_AddNoteToContact" IsBound="true">
<Parameter Name="entity" Type="mscrm.contact" Nullable="false" />
<Parameter Name="NoteTitle" Type="Edm.String" Nullable="false" Unicode="false" />
<Parameter Name="NoteText" Type="Edm.String" Nullable="false" Unicode="false" />
<ReturnType Type="mscrm.annotation" Nullable="false" />
</Action>
Unique Name: new_AddNoteToContact
Ref: https://msdn.microsoft.com/en-us/library/mt607600.aspx#Anchor_3
Update: If you would have edited the unique name after creating action, then duplicates will be created in Processes entity. Pls delete the dupes from Adv.Find
I fixed this some time ago but haven't got the time to get back to answer it. In my case what was the issue was the way i made the request it self
Instead of the following way, which is stated in the question:
Dim postData = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
Dim retrieveContactResponse As HttpResponseMessage = httpClient.PostAsync(requestUri, postData).Result
Instead of using the httpClient.PostAsync method and providing the StringContent object directly, I used the HttpRequestMessage object and giving that the StringContent object, and then provide the HttpRequestMessage object to the SendAsync method of the httpClient, and that seems to have solved my issue, as it is working now. Also notice that in the original question i had quotation marks in the value of the first JProperty in the JObject to be posted, I'm not sure this has anything to do with it though, but just posting it here as it is different from original code:
Dim jsonNote As JObject = New JObject(New JProperty("NoteTitle", "Mails have been deleted"), New JProperty("NoteText", "This contact's SmarterMail data has been deleted automatically due to inactivity on their CRM account"))
...
Dim reqMsg As New HttpRequestMessage(HttpMethod.Post, CRMWebApiUri + requestUri)
reqMsg.Content = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
Dim retrieveContactResponse As HttpResponseMessage = httpClient.SendAsync(reqMsg).Result

RestSharp Returning Unauthorized

I am having an issue and think I may be missing something with RestSharp.
I am authorizing and getting back a cookie just fine... see below. But then when I call to get the data it returns unauthorized. It works just fine in Postman but not in the code below. I am using a console app and I have tried to send the cookie via AddHeader, AddCookie, and just as a parameter. The responseLogin does contain the correct cookie. Any help would be great.
Dim clientLogin = New RestClient("http://[URI to Authorize]............")
Dim requestLogin = New RestRequest(Method.POST)
requestLogin.AddParameter("application/x-www-form-urlencoded", "[Username and password here.....]", ParameterType.RequestBody)
Dim responseLogin As IRestResponse = clientLogin.Execute(requestLogin)
Dim client = New RestClient("http://[URI to get data]............")
Dim request = New RestRequest(Method.GET)
request.AddHeader("Cookie", responseLogin.Cookies(0).Value.ToString)
request.AddHeader("Accept", "application/json")
Dim response As IRestResponse = client.Execute(request)
The Cookie header needs to contain the name and value for the cookie, e.g.
Dim authCookie = responseLogin.Cookies(0) ' Probably should find by name
request.AddHeader("Cookie", String.Format("{0}={1}", authCookie.Name, authCookie.Value))
However, the documentation (I've never used RestSharp personally) says that RestSharp has automatic support for cookies, so if you reuse the RestClient instance and set the CookieContainer you shouldn't need to do anything to handle the cookies manually (unless you want to, which in some cases may be preferable).
Dim client = New RestClient(New Uri("[Base URI...]"))
client.CookieContainer = New System.Net.CookieContainer()
Dim requestLogin = New RestRequest("[login page path]", Method.POST)
requestLogin.AddParameter("application/x-www-form-urlencoded", "[Username and password here.....]", ParameterType.RequestBody)
Dim responseLogin As IRestResponse = client.Execute(requestLogin)
Dim request = New RestRequest("[data api path", Method.GET)
request.AddHeader("Accept", "application/json")
Dim response As IRestResponse = client.Execute(request)
You could probably just reuse the cookie container with different RestClient instances instead of reusing the client.
I had the same issue on RestClient .NET framework 4.5.2 version.
It turns out you have to implement the IAuthenticator interface.
public class MyAuth : IAuthenticator
{
readonly string _password;
readonly string _passwordKey;
readonly string _username;
readonly string _usernameKey;
public MyAuth(string usernameKey, string username, string passwordKey, string password)
{
_usernameKey = usernameKey;
_username = username;
_passwordKey = passwordKey;
_password = password;
}
public void Authenticate(IRestClient client, IRestRequest request)
=> request
.AddCookie(_usernameKey, _username)
.AddCookie(_passwordKey, _password);
//.AddParameter(_usernameKey, _username)
//.AddParameter(_passwordKey, _password);
}
I did this and my request worked.

POST request in WP8 app

I'm creating a VB.NET app for Windows Phone 8 and I'm currently looking for a solution to send a POST request to a page, and to get the response (simply knowing the page content).
I searched on several forums but none of them helped me.
Thank you in advance
Before u have to download this NuGet package HttpClient for Windows Phone and try this code:
var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "http://www.google.com/");
var response = await httpClient.SendAsync(request);
var result = response.Content.ReadAsStringAsync().Result;
This will help you.
And u have to use in async method try this.
Async Function AccessTheWebAsync() As Task(Of Integer)
Dim client As HttpClient = New HttpClient()
Dim getStringTask As Task(Of String) = client.GetStringAsync("http://google.com")
Dim urlContents As String = Await getStringTask
Return urlContents.Length
End Function

windows 8 store FTP

I'm building a windows store application in VB.net, and when I try to get the certification, so I can publish it in the store, it fails the API test, because it doesn't accept the fact that I used AlexPilotti's FTPSClient dll.
The only thing I have to do is to upload a small video to the ftp server. Pretty simple, right? It's working properly, both in PC and Tablet, the only problem is the certification.
I had to use AlexPilotti's FTPSClient dll because "ftpwebrequest" is gone from Windows Store.
So, I wonder if there is anything else I can try, maybe a windows store compatible dll?
I have tried BackgroundTransfer, but apparently it only works with HTTP and HTTPS, not FTP.
I've been trying to find an answer for a few days now, but what I find never seems to work.
Thanks in advance!
Found this example by searching but didn't test it. Google is a programmers best friend.
Public Async Function FTP_Uploader(ftpURL As String, filename As String, username As String, password As String, UploadText As String) As Task(Of Boolean)
Try
Dim request As WebRequest = WebRequest.Create(ftpURL + "/" + filename)
request.Credentials = New System.Net.NetworkCredential(username.Trim(), password.Trim())
request.Method = "STOR"
Dim buffer As Byte() = Encoding.UTF8.GetBytes(UploadText)
Dim requestStream As Stream = Await request.GetRequestStreamAsync()
Await requestStream.WriteAsync(buffer, 0, buffer.Length)
Await requestStream.FlushAsync()
Return True
Catch ex As Exception
Return False
End Try End Function

Can somebody give me an example of getting the content of a page from a URL in vb.net for windows 8?

I am very new to vb/.net and I'm trying to do something that I can do easily in classic vb. I want to get the source html for a webpage from the URL.
I'm using vb.net in Visual Studio Express for Windows 8.
I've read loads of stuff that talk about HttpWebRequest, but I can't get it to work properly.
I did at one point have it returning the html header, but I want to content of the page. Now, I can't even get it back to giving me the header. Ultimately, I want to process the html returned which I'll do (to begin with) the old-fashioned way and process the returned html as a string, but for now I'd like to just get the page.
The code I've got is:
Dim URL As String = "http://www.crayola.com/"
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(New Uri(URL))
txtHTML.Text = request.GetRequestStreamAsync().ToString()
Can anyone help me with an example to get me going please?
You're trying to use an Async method in a synchronous way, which won't make any sense. If you're using .NET 4.5, you can try marking the calling method with Async and then using the Await keyword when calling GetRequestStreamAsync.
Public Sub MyDownloaderMethod()
Dim URL As String = "http://www.crayola.com/"
Dim request As System.Net.HttpWebRequest
= System.Net.HttpWebRequest.Create(New Uri(URL))
' Use the Await keyword wait for the async task to complete.
Dim response = request.GetResponseAsync()
txtHTML.Text = response.GetResponseStream().ToString()
End Function
See the following MSDN article for more information on async programming with the Await keyword: http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx
Edit
You are receiving your error because you're trying to get the Request stream (what you send the server), and what you really want is the Response stream (what the server sends back to you). I've updated my code to get the WebResponse from your WebRequest and then retrieve the stream from that.
Public Shared Function GetWebPageString(ByVal address As Uri) As String
Using client As New Net.WebClient()
Return client.DownloadString(address)
End Using
End Function
There is also DownloadStringAsync if you don't want to block
request.GetRequestStreamAsync() is probably not a method. I think you're cribbing code from a site where someone wrote their own add-on methods to HttpWebRequest. Try request.GetResponse() to return a response object, then in the response object you can inspect the stream and convert it to text if you need to.
This worked for me in VB.Net 4.5
Public Async Sub GetHTML()
Dim PageHTML as string
Dim client As New HttpClient
Dim getStringTask As Task(Of String) = client.GetStringAsync(PageURL)
PageHTML = Await getStringTask
MsgBox(PageHTML)
End Sub