2017-02-23 161 views
0

我有PHP代碼,其中CURL被用於PHP頁面中的某些API命中。並且該頁面正在通過Ajax命中訪問,如下所示。這裏是代碼:將PHP CURL轉換爲C#

AJAX命中

$.ajax({ 
type: "POST",  
url: 'lib/ssss.php', 
data: {username: username, userpassword:userpassword, token:token}, 
dataType: 'json', 
success: function(data){ 
    if (data[0].errormessage=="success"){ 
     $("#memberDetails").show(); 
     $('#compform').deserialize({ 
     fighterfirstname: "" + data[0].firstname + "", 
     fighterfirstnamedisplay: "" + data[0].firstname + "", 
     fightersurname: "" + data[0].lastname + "", 
     fightersurnamedisplay: "" + data[0].lastname + "", 
     competitorid: "" + data[0].memberid + "", 
     competitoriddisplay: "" + data[0].memberid + "", 
     academy: "" + data[0].academy + "", 
     academydisplay: "" + data[0].academy + "", 
     phone: "" + data[0].phone + "", 
     phonedisplay: "" + data[0].phone + "", 
     email: "" + data[0].email + "", 
     dob: "" + data[0].dob + "" 
     });       

     if (tshirt.length>0){ 
      $('#tshirtDiv').show(); 
      $('#tshirt').selectOptions("Unchosen"); 
     } 

     checkDob2(data[0].dob, cutOffDob); 
     $("#confirmEmail").focus(); 

     } 

     if (data[0].errormessage=="Authentication Failed"){ 
      $("#submittToAfbjj_error").css('color', 'red'); 
      $("#submittToAfbjj_error").show().html("AFBJJ member authentication unsuccessful"); 
      cleareventform(); 
      $("#memberDetails").hide(); 
     } 

     if(data[0].errormessage=="Session exired" || 
      data[0].errormessage=="Invalid tokens" || 
      data[0].errormessage=="Token mismatch"){ 
      alert("Your session has expired, hit OK to reload page."); 
      location.reload(); 
     } 
    } 
}); 

捲曲的頁面代碼:

$username = $_POST['username']; 
     $userpassword = $_POST['userpassword']; 

     $fields = array("username" => $username, "userpassword" => $userpassword); 
     $fields_string = ''; 
     foreach ($fields as $key => $value) { 
      $fields_string .= $key . '=' . $value . '&'; 
     } 
     $fields_string = rtrim($fields_string, '&'); 

     $url = 'https://test.com/remote.php'; 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     $result = curl_exec($ch); 


     curl_close($ch); 

    // echo json_encode($fields_string); 
    echo $result; 

我曾嘗試下面RestClientConvert CURL into C#它retruning 「OK」 的狀態,但不返回數據如在PHP中。請幫幫我。

我曾嘗試下面的代碼:

var client = new RestClient("https://test.com/remote.php"); 
     var request = new RestRequest(Method.POST); 
     request.AddHeader("content-type", "application/x-www-form-urlencoded"); 
     request.AddHeader("cache-control", "no-cache"); 
     request.AddHeader("header1", "headerval"); 
     request.AddParameter("application/x-www-form-urlencoded", "username=anton5", ParameterType.RequestBody); 
     request.AddParameter("application/x-www-form-urlencoded", "userpassword=anton", ParameterType.RequestBody); 

     IRestResponse response = client.Execute(request); 
+0

C#代碼,語法錯誤。 #predictions http://stackoverflow.com/help/how-to-ask – Twinfriends

+0

看這裏多麼簡單的發送請求在C# http://stackoverflow.com/questions/4015324/http-request-with-post – 2017-02-23 09:46:15

回答

0

如何簡單的HTTP POST數據發送到服務器(API):第27行

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net; 
using System.Collections.Specialized; 

namespace HttpReq 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       using (var client = new WebClient()) 
       { 
        var postData = new NameValueCollection(); 
        postData["User"] = "Username"; 
        postData["Pass"] = "Password"; 
        var response = client.UploadValues("https://example.com/api.php", postData); 
        var data = Encoding.Default.GetString(response); 
        Console.WriteLine("Data from server: " + data); 
        // Wait for key 
        Console.ReadKey(); 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e); 
       Console.ReadKey(); 
      } 
     } 
    } 
}