how to retrieve post httpresponse data - asp.net

I have a part of code that sends a post httprequest to weburl and then the response is again in post method , how can I get the posted data from httpresponse :
Dim request1 As HttpWebRequest = HttpWebRequest.Create("some url here" & str)
request1.Method = "POST"
request1.CookieContainer = cookie
enc = New System.Text.UTF8Encoding()
postdatabytes = enc.GetBytes(postdt)
request1.ContentType = "application/x-www-form-urlencoded"
' request1.ContentType = "application/json"
request1.ContentLength = postdatabytes.Length
'Dim strn As New StreamWriter(request1.GetRequestStream())
'strn.Write(postdt)
'strn.Close()
Using stream = request1.GetRequestStream()
stream.Write(postdatabytes, 0, postdatabytes.Length)
End Using
Dim r As String
Dim response1 As HttpWebResponse = request1.GetResponse()
If response1.Method = "POST" Then
'' here I want to get response data
End If
After a long searching I didnt find any answer ...

Related

How to read POST data sent from HttpWebRequest

Dim request As WebRequest = WebRequest.Create("http://www.example.com/a.aspx ")
request.Method = "POST"
Dim postData = Encoding.UTF8.GetBytes("example")
request.ContentLength = postData.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(postData, 0, postData.Length)
dataStream.Close()
Examples like above can be found everywhere, but how about the other side - how to retrieve and process the request data (i.e."example")?

Soap Request : 500 - Internal Server Error

This is my code it is in vb.net . I did'nt get response from soap request , i have no idea what is the wrong with this code , its showing 500 Internal Server Error.
Dim webRequest__1 As WebRequest = WebRequest.Create("https://login.twinfield.com/webservices/session.asmx?wsdl")
Dim httpRequest As HttpWebRequest = DirectCast(webRequest__1, HttpWebRequest)
httpRequest.Method = "POST"
httpRequest.ContentType = "application/soap+xml;charset=UTF-8;action='http://www.twinfield.com/Logon'"
httpRequest.Host = "login.twinfield.com"
httpRequest.Headers.Add("SOAPAction:https://login.twinfield.com/webservices/session.asmx")
httpRequest.ProtocolVersion = HttpVersion.Version11
httpRequest.Credentials = CredentialCache.DefaultCredentials
Dim requestStream As Stream = httpRequest.GetRequestStream()
'Create Stream and Complete Request
Dim streamWriter As New StreamWriter(requestStream, Encoding.ASCII)
' <soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:twin='http://www.twinfield.com/'>
Dim soapRequest As New StringBuilder("<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:twin='http://www.twinfield.com/'><soap:Header/><soap:Body><twin:Logon><twin:user>dgf</twin:user> <twin:password>cfg</twin:password><twin:organisation>dfd</twin:organisation></twin:Logon></soap:Body></soap:Envelope>")
'soapRequest.Append(" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" ")
'soapRequest.Append("xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body>")
'soapRequest.Append("<GetMyName xmlns=""http://tempuri.org/""><name>Sam</name></GetMyName>")
'soapRequest.Append("</soap:Body></soap:Envelope>")
streamWriter.Write(soapRequest.ToString())
streamWriter.Close()
'Get the Response
Dim htttpresponse As HttpWebResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
'Dim wr As HttpWebResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
'DirectCast(httpRequest.GetResponse(), HttpWebResponse)
Dim srd As New StreamReader(htttpresponse.GetResponseStream())
Dim resulXmlFromWebService As String = srd.ReadToEnd()
'Return resulXmlFromWebService
To call a soap web service, I think it is better to add the service url as a web reference (i.e. name as twinfield) in visual studio. Then you can use the service as a function as below.
twinfield.Session s = new twinfield.Session();
twinfield.LogonAction ac;
string cl = "";
twinfield.LogonResult k = s.Logon("dsfa", "Sadfsa", "sdfas", out ac, out cl);

Curl post from asp.net/vb

I'm trying to take the following curl command and use it in my vb.net page to connect to 3rd party.
curl -X POST -i -H "Accept: application/json" -u someusername:somepassword "https://domain.com/xyz/api/user" -d 'firstName=john&lastName=smith&ssn=111223333&currentAddress=1 Main ST&currentCity=somecity&currentState=WA&currentZip=99999'
Command above work fine in cygwin and I've tried to convert it to following but no luck. I tried encoding the password but that didn't help either. I would appreciate any help with this. Thank you!
Dim url As String = "https://domain.com/xyz/api/user"
Dim data As String = "{""CurrentAddress"":""1 Main St"", ""CurrentCity"":""Somecity"",""CurrentState"":""WA"",""CurrentZip"":""99999"",""FirstName"":""john"",""LastName"":""smith"",""ssn"":""111223333""}"
Dim myReq As WebRequest = WebRequest.Create(url)
myReq.Method = "POST"
myReq.ContentLength = data.Length
myReq.ContentType = "application/json"
myReq.Credentials = New NetworkCredential("someusername", "somepassword")
Using ds As Stream = myReq.GetRequestStream()
ds.Write(System.Text.ASCIIEncoding.Default.GetBytes(data), 0, data.Length)
End Using
Dim wr As WebResponse = myReq.GetResponse()
Dim receiveStream As Stream = wr.GetResponseStream()
Dim reader As New StreamReader(receiveStream, Encoding.UTF8)
Dim content As String = reader.ReadToEnd()
MsgBox(content)
After messing with it for two days, I was able to get it to work. here is the solution that I came up with.
Dim paramName As String() = New String(6) {"firstName", "lastName", "ssn", "currentAddress", "currentCity", "currentState", "currentZip"}
Dim paramVal As String() = New String(6) {"John", "Smith", "111223333", "1 main st", "any city", "dc", "11111"}
Dim result As String
result = HttpPost("https://domain.com/XYZ/api/user", paramName, paramVal)
Private Shared Function HttpPost(url As String, paramName As String(), paramVal As String()) As String
Dim req As HttpWebRequest = TryCast(WebRequest.Create(New Uri(url)), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
req.Headers("Authorization") = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("myusername:mypassword"))
' Build a string with all the params, properly encoded.
' We assume that the arrays paramName and paramVal are
' of equal length:
Dim paramz As New StringBuilder()
For i As Integer = 0 To paramName.Length - 1
paramz.Append(paramName(i))
paramz.Append("=")
paramz.Append(HttpUtility.UrlEncode(paramVal(i)))
paramz.Append("&")
Next
' Encode the parameters as form data:
Dim formData As Byte() = UTF8Encoding.UTF8.GetBytes(paramz.ToString())
req.ContentLength = formData.Length
' Send the request:
Using post As Stream = req.GetRequestStream()
post.Write(formData, 0, formData.Length)
End Using
' Pick up the response:
Dim result As String = Nothing
Using resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(resp.GetResponseStream())
result = reader.ReadToEnd()
End Using
Return result
End Function

HTTP post request failed from sending on server

I'm trying to post these variable threw an engine which gives back a responce but while testing it localy on VStudio everything is normal. When i upload it on the server the post request are not precessed.
Dim webStream As Stream
Dim s As HttpWebRequest`enter code here`
Dim res As HttpWebResponse
Dim webResponse = ""
Dim enc As UTF8Encoding
Dim postdata As String
Dim postdatabytes As Byte()
s = HttpWebRequest.Create("http://private.pin-pay.com:3902/")
enc = New System.Text.UTF8Encoding()
postdata = "PN=961" & ddlPhone.SelectedValue & txtMobilePhone.Text & "&TI=" & TransactionID.Text
postdatabytes = enc.GetBytes(postdata)
s.Method = "POST"
s.ContentType = "application/x-www-form-urlencoded"
s.ContentLength = postdatabytes.Length
Using stream = s.GetRequestStream()
stream.Write(postdatabytes, 0, postdatabytes.Length)
End Using
res = s.GetResponse()
webStream = res.GetResponseStream()
Dim webStreamReader As New StreamReader(webStream)
While webStreamReader.Peek >= 0
webResponse = webStreamReader.ReadToEnd()
End While

How to create a request to a URL and write the response to the page

I believe I've done this before, but I can't seem to remember how:(
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.example.com");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
How do I then write the response to the page?
Thanks a lot.
HttpWebRequest r = ( HttpWebRequest)WebRequest.Create("http://www.demo.com");
r.Method = "Get";
HttpWebResponse res = (HttpWebResponse)r.GetResponse();
Stream sr= res.GetResponseStream();
StreamReader sre = new StreamReader(sr);
string s= sre.ReadToEnd();
Response.Write(s);
The below example demonstrates how it can be done:
string myRequest = "abc=1&pqr=2&lmn=3";
string myResponse="";
string myUrl = "Where you want to post data";
System.IO.StreamWriter myWriter = null;// it will open a http connection with provided url
System.Net.HttpWebRequest objRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(myUrl);//send data using objxmlhttp object
objRequest.Method = "GET";
objRequest.ContentLength = TranRequest.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";//to set content type
myWriter = new System.IO.StreamWriter(objRequest.GetRequestStream());
myWriter.Write(myRequest);//send data
myWriter.Close();//closed the myWriter object
System.Net.HttpWebResponse objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();//receive the responce from objxmlhttp object
using (System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream()))
{
myResponse= sr.ReadToEnd();
}
Then u can use the data in myResponse to display whatever is returned.
Hope this helps you...

Resources