c# - Response is null in PHP while .Net is giving all text -
i'm using curl in php id in response i'm getting empty response. here code:
$jsoncontent = "{\"param1\" : [{\"subparam1\" : subnumval1,\"subparam2\" : subnumval2 },{\"subparam1\" : subnumval1,\"subparam2\" : subnumval2 }],\"param2\" : \"val2\",\"param3\" : \"val3\"}"; $url = https_url; $ch = curl_init($url); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); curl_setopt($ch, curlopt_userpwd, "user:pass"); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $jsoncontent); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_httpheader, array( 'content-type: application/json', 'content-length: ' . strlen($jsoncontent)) ); $output = curl_exec($ch); if($output === false) { echo 'curl error: ' . curl_error($ch); }else { echo $output; }
while i'm trying response using .net (c#),i'm getting response, here .net code:
public partial class postdata : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (!page.ispostback) postblockfacedata(); } // post json string void postblockfacedata() { //create credentials cache can more 1 credentialcache tempcache= new credentialcache(); //create network username , password networkcredential securecred = new networkcredential("user", "pass"); //add login credentials credential cache parkmecache.add(new uri("https_url:443/"), "basic", securecred); //json array in string format string jsoncontent = "{\"param1\" : [{\"subparam1\" : "subval1",\"subparam2\" : "subval2" },{\"subparam1\" : "subval2",\"subparam2\" : "subparam2" }],\"param2\" : \"val2\",\"param3\" : \"val3\"}"; // string url = https_url //create webrequest url httpwebrequest request = (httpwebrequest)webrequest.create(url); request.preauthenticate = true; //use authentication request.usedefaultcredentials = true; //use default credential available request.credentials = parkmecache; //pass credential cache request object //the next used ignore bit matching system.net.servicepointmanager.servercertificatevalidationcallback = delegate( object sender1, x509certificate certificate, x509chain chain, system.net.security.sslpolicyerrors sslpolicyerrors) { homecoming true; }; request.method = "post"; //http verb request.contentlength = jsoncontent.length; //json info string length request.contenttype = @"application/json"; //the content type of application in json format system.text.utf8encoding encoding = new system.text.utf8encoding(); //specified utf8 standard text coding byte[] bytearray = encoding.getbytes(jsoncontent); //converted json info bytes because datastream understands way using (stream datastream = request.getrequeststream()) //get request stream handle { datastream.write(bytearray, 0, bytearray.length); //write json info request stream } long length = 0; string strstatuscode=""; string strdesc = ""; seek { using (httpwebresponse response = (httpwebresponse)request.getresponse()) //execute request , response phone call { length = response.contentlength; //length of content strstatuscode = response.statuscode.tostring(); //return status code ok on success or status error codes 400, 403 etc., strdesc = response.statusdescription; //the description has homecoming value in case on success returns block face id response.write("status code : " + strstatuscode + " , status description : " + strdesc); } } grab (webexception ex) { response.write(ex.message); // log exception , throw illustration above } } }
above code working charm..i don't know where's issue in php. can help me?
$jsoncontent = "{\"param1\" : [{\"subparam1\" : "subval1",\"subparam2\" : "subval2" },{\"subparam1\" : "subval2",\"subparam2\" : "subparam2" }],\"param2\" : \"val2\",\"param3\" : \"val3\"}";
to
$jsoncontent = "{\"param1\" : [{\"subparam1\" : \"subval1\",\"subparam2\" : \"subval2\" },{\"subparam1\" : \"subval2\",\"subparam2\" : \"subparam2\" }],\"param2\" : \"val2\",\"param3\" : \"val3\"}";
you have not escaped double quotes.
c# php curl
No comments:
Post a Comment