In Silverlight HttpWebRequest does not contain definition for GetResponse() - silverlight-4.0

I am working on SL4 and when I run following code it gives me the error as
HttpWebRequest does not contain definition for GetResponse().
Then how to get the response.
string str = "http://sam.com/getdeatils.php";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(str);
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
string end = stream Reader.ReadToEnd();

It s because silverlight work asyncrously have at rest sharp. Hope it help

Related

Send Json POST Method HttpWebRequest Vb.NET

I already have how to send a JSON by the POST method in Vb.NET, here I leave the code:
Dim request As HttpWebRequest = HttpWebRequest.Create("myurl")
request.Method = "POST"
request.ContentType = "application/json"
request.Headers.Add("authorization", "Bearer 80mgkm6D60OtY16pzs93WoYmx2kzTgf3CELERMVg")
Dim PostString As String = JsonConvert.SerializeObject(MyClase)
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(PostString)
request.ContentLength = byteArray.Length
Dim dataStream1 As Stream = request.GetRequestStream()
dataStream1.Write(byteArray, 0, byteArray.Length)
dataStream1.Close() 'sends request
Question: Is there a simpler way (less code) to do the same thing.
I thank you very much
Question: Is there a simpler way (less code) to do the same thing.
httpClient is what you should be using for new development. Note that HttpClient should NOT be wrapped in a using block.
Public client as new HttpClient()
Public Function makeHttpRequest()
Try
client.DefaultRequestHeaders.Add("HEADERNAME", "HEADERVALUE")
Using response As HttpResponseMessage = Await client.PostAsync("url", new StringContent("YourJsonString", Encoding.UTF8, "application/json"))
Dim responseBody As String = Await response.Content.ReadAsStringAsync()
End Using
Catch e As HttpRequestException
'handle exceptions
End Try
End function
Thank you very much it was solved with the HttpClient library
In the previous way, the error 422 came out in the API server and the JSON was not processed
Dim client As HttpClient = New HttpClient()
Dim request_json = MyJSON
Dim content = New StringContent(request_json, Encoding.UTF8, "application/json")
client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", myToken)
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
Dim result = client.PostAsync("myurl", content)
Dim result_Json_string = result.Result.Content.ReadAsStringAsync()
Thank you very much for your advice.

VB.NET File upload using WebApi: UnExpected end of MIME

Everything seems to be working except I get this below error in the WEBAPI:
Unexpected end of MIME multipart stream. MIME multipart message is not complete.
When I upload the same file from postman to the WEBAPI endpoint it works perfectly. Makes me think its a problem with the VB application.
BTW: Not skilled in VB (Inherited Application trying to make it work to upload large files)
Dim fileBytes As Byte() = Nothing
Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")
Using binaryReader As New BinaryReader(fileUpload1.PostedFile.InputStream)
fileBytes = binaryReader.ReadBytes(binaryReader.BaseStream.Length)
binaryReader.Close()
End Using
Dim request As HttpWebRequest =
WebRequest.Create("http://mylocalhost/api/directory")
request.Method = "POST"
request.ContentType = "multipart/form-data; boundary = " + boundary
request.ContentLength = fileBytes.Length
request.Accept = "*/*"
request.AutomaticDecompression = DecompressionMethods.GZip
request.KeepAlive = True
Dim newStream As IO.Stream = request.GetRequestStream()
newStream.Write(fileBytes, 0, fileBytes.Length)
'newStream.Close()
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As IO.Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
Debug.WriteLine("File uploaded")
Return True````

API-Problem - POST Json-String with httpClient in VB.net

I want to POST json values via httpClient into an API. Unfortunately I am quite a beginner and can't handle it.
GET works perfectly:
here the code for GET:
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
' Wird für https-Requests benötigt
System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
request = DirectCast(WebRequest.Create(url), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim s As String
s = reader.ReadToEnd
Return s
But how do I realize POST?
My API-URL looks like this:
https://mydomaine.net/api/auth?token=467445892587458542...
an i will send an JSON-String {"test":"value1":"test1":"value2"}
I've been working on this problem for many hours.
Can someone please help ???
and here is the code that works
Public Function PostRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
Dim response As String
Dim request As HttpWebRequest
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
request = WebRequest.Create(uri)
request.ContentLength = jsonDataBytes.Length
request.ContentType = contentType
request.Method = method
Using requestStream = request.GetRequestStream
requestStream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
requestStream.Close()
Using responseStream = request.GetResponse.GetResponseStream
Using reader As New StreamReader(responseStream)
response = reader.ReadToEnd()
End Using
End Using
End Using
Return response
End Function

HttpWebRequest with response

I want make an HttpWebRequest and get one response, this is my code.
Dim inStream As StreamReader
Dim webRequest As WebRequest
Dim webresponse As WebResponse
Dim request As HttpWebRequest = webRequest.Create("https:/x.x.x")
request.CookieContainer = New CookieContainer
request.Credentials = New NetworkCredential(login, "password")
Dim respone = request.GetResponse()
inStream = New StreamReader(respone.GetResponseStream())
TextBox1.Text = inStream.ReadToEnd
But I received 2 responses. One is okay but the second is without session. Can I filter the responses and get only one?

continuous http stream

My app currently sends GET httpwberequests and parses the content after reading the response stream for each request and the repeats this process over and over. I need to implement a continuous http stream which uses the same GET request but the connection stays open, from what I understand. how would I change my existing code to do this?
Dim responseReader As StreamReader = Nothing
Dim Reader As StreamReader = Nothing
Dim responseData As String = ""
Dim webresponse As HttpWebResponse = Nothing
Dim responseStream As Stream = Nothing
Dim returnContent As String = Nothing
Dim cdata As Integer = 0
webRequest = TryCast(System.Net.WebRequest.Create(url), HttpWebRequest)
Try
webresponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
responseStream = webresponse.GetResponseStream
If webresponse.ContentEncoding.ToLower().Contains("gzip") Then
responseStream = New GZipStream(responseStream, CompressionMode.Decompress)
ElseIf webresponse.ContentEncoding.ToLower().Contains("deflate") Then
responseStream = New DeflateStream(responseStream, CompressionMode.Decompress)
End If
Reader = New StreamReader(responseStream, Encoding.Default)
responseData = Reader.ReadToEnd()
Catch
Throw
Finally
webRequest.GetResponse().GetResponseStream().Close()
Reader.close()
End Try
Return responseData
The streaming HTTP implementation allows one to cosume data in real time over a single persistent HTTp request. It access a data feed that continues to send activities on that response. I need to read data from the response, process it and then continue reading from the response and continue this process until the connection is closed.
Would this be like a asynchronous request instead?
This is actually one of the goals of HTML5. You can take a look at http://socket.io/ to see implementations that work for HTML4 browsers