将PHP CURL转换为C#
问题描述:
我有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);
答
如何简单的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();
}
}
}
}
C#代码,语法错误。 #predictions http://stackoverflow.com/help/how-to-ask – Twinfriends
看这里多么简单的发送请求在C# http://stackoverflow.com/questions/4015324/http-request-with-post – 2017-02-23 09:46:15