GET &のPOSTリクエストと特別なパラメータの両方を受け入れるPHP + Laravelフレームワークを使用してサーバー側でAPIを作成しました。例えば、それはなります、私たちはこれらのパラメータを持つGETメソッドでリクエストを送信する場合POSTまたはC#windowsのJsonレスポンス付きリクエストを取得するフォームアプリケーション
token > string ,
restaurant_id > integer ,
admin_id > integer ,
token_id > integer .
:http://beresun.ir/API/Orders/0
、それはこれらのパラメータを取得します。
それのアドレスがある
http://beresun.ir/API/Orders/0?token=2JEuksuv86DcFmLrQa7nna4QDeowuGTqpyUK0pf9wSlbe6D5hLtEVxvzMT5gAZG0xBKy00HxS3J79mcr8F54dBD0uIg5HX5fzPOAP&restaurant_id=1&admin_id=2&token_id=40ましたjsonデータを返します。リンクをクリックすると結果が表示されます。
応答jsonデータには、顧客とその製品に関する情報が含まれています。
今私がPOSTで、このAPIからC#と要求データと、このサービスのためのWindowsアプリケーションを作成したりする方法を取得したい:
私は、WebサーバーからJSONデータを取得し、それらを保存するには、このAPIを使用したいです私のWindowsアプリケーション、だから私は自分のフォームクラスの一つで二つの機能を作成しました:今、ここで
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
public partial class MainActivity : Form
{
string token = "2JEuksuv86DcFmLrQa7nna4QDeowuGTqpyUK0pf9wSlbe6D5hLtEVxvzMT5gAZG0xBKy00HxS3J79mcr8F54dBD0uIg5HX5fzPOAP";
int restaurant_id = 1;
int admin_id = 2;
int token_id = 40;
private void SendWebrequest_Get_Method()
{
try
{
String postData = "token=" + token +
"&restaurant_id=" + restaurant_id +
"&admin_id=" + admin_id +
"&token_id=" + token_id;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://beresun.ir/API/Orders/0?" + postData);
request.ContentType = "application/json; charset=utf-8";
request.Accept = "application/json";
request.Method = WebRequestMethods.Http.Get;
WebResponse response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
String json_text = sr.ReadToEnd();
dynamic stuff = JsonConvert.DeserializeObject(json_text);
if (stuff.error != null)
{
MessageBox.Show("problem with getting data", "Error");
}
else
{
MessageBox.Show(json_text, "success");
}
sr.Close();
}
catch (Exception ex)
{
MessageBox.Show("Wrong request ! " + ex.Message, "Error");
}
}
private void SendWebrequest_POST_Method()
{
try
{
// Create a request using a URL that can receive a post.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://beresun.ir/API/Orders/5");
// Set the Method property of the request to POST.
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
// Create POST data and convert it to a byte array.
string postData = "token=" + token +
"&restaurant_id=" + restaurant_id +
"&admin_id=" + admin_id +
"&token_id=" + token_id;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json; charset=utf-8";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
MessageBox.Show(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
MessageBox.Show(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show("Wrong request ! " + ex.Message, "Error");
}
}
}
私はAPIをテストするとき、それは正常に動作しますが、私は自分のアプリケーションからのデータを要求したとき、それはエラーを返し、問題となっています働いていない。
このAPIからデータを要求し、データを取得し、たくさん検索し、多くの異なるメソッドを使用していましたが、どれも私のために働かなかったのです。このAPIが非常に多くのJsonデータを返すか、タイムアウトが発生する可能性があります。私は知らない、私は問題を見つけることができませんでした。だから私はここにそれを尋ねた。
私は何をすべきかわかりません。
おかげ
おかげでブロ、本当にありがとうございました。 :) あなたは私の命を救いました 。 あなたがアンドロイドプログラミングやウェブ開発(php、laravelのもの)に関するご質問がある場合は、私にメールを送ってください。peyman.forceman @ gmail。 com。 ありがとう –