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")?
Related
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 ...
Am new to .Net and web service..i like to pass id through url..how to do that?whether it was done post or get method?guide me
string url = "http://XXXXX//"+id=22;
WebRequest request = WebRequest.Create(url);
request.Proxy.Credentials = new NetworkCredential(xxxxx);
request.Credentials = CredentialCache.DefaultCredentials;
//add properties
request.Method = "GET";
request.ContentType = "application/json";
//convert
byte[] byteArray = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteArray.Length;
//post data
Stream streamdata = request.GetRequestStream();
streamdata.Write(byteArray, 0, byteArray.Length);
streamdata.Close();
//response
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Clean up the streams and the response.
reader.Close();
response.Close();
If you just want to build the URL with parameters (ref: Query String) then you can simply do:
string url = string.Format("http://www.google.com/?id={0}",22);
You can check this url, Passing variables between pages using QueryString at Code Project
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
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...
I am writing a Web service client in C# which takes the URL of the web Service, and a web method name.
I want to check if thew Web method actually receives an int and returns a DataTable, and if this is true, it should call it and return the DataTable.
I have found a couple of posts where this is accomplished compiling dynamically the Proxy class.
But for my case this'd be too expensive, because the client is actually a WSS WebPart, and I don't want to do this every time the page is rendered; only when the properties are changed.
In the end of the day web service description is just an XML file. You can grab it by requesting service.asmx?WSDL:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:1753/Service1.asmx?WSDL");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string xml = reader.ReadToEnd();
Once you have service description you can parse it and check method signature. Then, you can invoke the method using HTTP POST:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:1753/Service1.asmx?HelloWorld");
request.Method = "POST";
request.ContentType = "application/soap+xml; charset=utf-8";
byte[] data = Encoding.UTF8.GetBytes(
#"<?xml version='1.0' encoding='utf-8'?>
<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'>
<soap12:Body>
<HelloWorld xmlns='http://tempuri.org/' />
</soap12:Body>
</soap12:Envelope>");
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string xml = reader.ReadToEnd();
If the Web Service is always the same (i.e. the method is the same as well as what it returns) and uit's just the url that might change, then just add a web reference to the project with the webpart in it, the set the Url of the proxy like so:
using (var serviceProxy = new ServiceProxy())
{
serviceProxy.Url = somepropertysetbythewebpart;
// make call to method here
}
After a lot of resarching I found out how to do it. The code of the selected answer is almost there, but I had to add the SOAPAction in the header and also change the ContentType.
Here is the entire code:
var strRequest = #"<soap12:Envelope>
...
</soap12:Envelope>";
string webServiceUrl = "http://localhost:8080/AccontService.svc";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webServiceUrl);
request.Method = "POST";
request.ContentType = "text/xml;charset=UTF-8";
request.Accept = "text/xml";
request.Headers.Add("SOAPAction", "http://tempuri.org/IAccountService/UpdateAccount");
byte[] data = Encoding.UTF8.GetBytes(strRequest);
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseXmlString = reader.ReadToEnd();
return new HttpResponseMessage()
{
Content = new StringContent(responseXmlString, Encoding.UTF8, "application/xml")
};