public partial class PostData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PostBlockFaceData();
}
// POST a JSON string
void PostBlockFaceData()
{
//Create the credentials cache as you can more than one
CredentialCache tempCache= new CredentialCache();
//Create the Network username and password
NetworkCredential secureCred = new NetworkCredential("user", "pass");
//Add the login credentials to the 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 the Webrequest with the URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.PreAuthenticate = true; //use authentication
request.UseDefaultCredentials = true; //Use the default credential available
request.Credentials = parkmeCache; //pass the credential cache to the request object
//The following is to used to ignore bit matching
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(
Object sender1,
X509Certificate certificate,
X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
request.Method = "POST"; //HTTP VERB
request.ContentLength = jsonContent.Length; //JSON DATA string length
request.ContentType = @"application/json"; //The Content type is of application in json format
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); //specified UTF8 standard text coding
Byte[] byteArray = encoding.GetBytes(jsonContent); //converted the json data to bytes because datastream understands that way
using (Stream dataStream = request.GetRequestStream()) //get the request stream handle
{
dataStream.Write(byteArray, 0, byteArray.Length); //write the json data to the request stream
}
long length = 0;
string strStatusCode="";
string strDesc = "";
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //execute the request and get the response from the call
{
length = response.ContentLength; //length of the content
strStatusCode = response.StatusCode.ToString(); //return the status code OK on success or status error codes like 400, 403 etc.,
strDesc = response.StatusDescription; //The description has the return value in this case on success returns the Block Face ID
Response.Write("Status Code : " + strStatusCode + " , Status Description : " + strDesc);
}
}
catch (WebException ex)
{
Response.Write(ex.Message);
// Log exception and throw as for GET example above
}
}
}
Приведенный выше код работает просто великолепно. Я не знаю, в чем проблема в PHP. Кто-нибудь может мне помочь?
Пока я пытаюсь получить ответ с помощью .NET (C#), я получаю ответ, вот код .NET:
[code]public partial class PostData : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) PostBlockFaceData();
}
// POST a JSON string void PostBlockFaceData() { //Create the credentials cache as you can more than one CredentialCache tempCache= new CredentialCache();
//Create the Network username and password NetworkCredential secureCred = new NetworkCredential("user", "pass");
//Add the login credentials to the credential cache parkmeCache.Add(new Uri("HTTPS_URL:443/"), "Basic", secureCred);
//Create the Webrequest with the URL HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.PreAuthenticate = true; //use authentication request.UseDefaultCredentials = true; //Use the default credential available request.Credentials = parkmeCache; //pass the credential cache to the request object
//The following is to used to ignore bit matching System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate( Object sender1, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };
request.Method = "POST"; //HTTP VERB request.ContentLength = jsonContent.Length; //JSON DATA string length request.ContentType = @"application/json"; //The Content type is of application in json format System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); //specified UTF8 standard text coding Byte[] byteArray = encoding.GetBytes(jsonContent); //converted the json data to bytes because datastream understands that way
using (Stream dataStream = request.GetRequestStream()) //get the request stream handle { dataStream.Write(byteArray, 0, byteArray.Length); //write the json data to the request stream } long length = 0; string strStatusCode=""; string strDesc = ""; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //execute the request and get the response from the call {
length = response.ContentLength; //length of the content strStatusCode = response.StatusCode.ToString(); //return the status code OK on success or status error codes like 400, 403 etc., strDesc = response.StatusDescription; //The description has the return value in this case on success returns the Block Face ID Response.Write("Status Code : " + strStatusCode + " , Status Description : " + strDesc); } } catch (WebException ex) { Response.Write(ex.Message); // Log exception and throw as for GET example above } }
} [/code]
Приведенный выше код работает просто великолепно. Я не знаю, в чем проблема в PHP. Кто-нибудь может мне помочь?