C# HTTP request to PHP file to JSON -
i write class in c# should send http request (post) php file on server in order retrieve json object.
this code i've got:
public void sendrequest(){ httpwebrequest request = (httpwebrequest) webrequest.create("url"); // execute request httpwebresponse response = (httpwebresponse) request.getresponse(); }
is need? think should change or improve? thank help.
you need post data , read response:
httpwebrequest request = (httpwebrequest)webrequest.create("url"); string yourpostdata = "your post data"; string sreverresponsetext; byte[] postdatabytes = encoding.utf8.getbytes(yourpostdata); request.contentlength = yourpostdata.length; request.contenttype = "application/x-www-form-urlencoded"; request.method = "post"; using (stream requeststream = request.getrequeststream()) requeststream.write(postdatabytes, 0, postdatabytes.length); using (response = (httpwebresponse)request.getresponse()) using (streamreader streamreader = new streamreader(response.getresponsestream())) sreverresponsetext = streamreader.readtoend();
now looking in sreverresponsetext
, can access headers response.headers.tostring()
Comments
Post a Comment