私はユーザーがデータベースに挿入することができますApiがあります。私はWindowsフォームアプリケーションでApiを要求しようとしていますが、System.Net.WebExceptionを取得し続けています: 'リモートサーバーはエラーを返しました:(403)Forbidden.AI比較的新しいAPIリクエストです。誰かが私が間違ってやっていることを見ることができます、ありがとう。CでのHttpWebRequestのPOST#
Customerクラス:
public class Customer
{
public int StoreCustomerID { get; set; }
// Actaul customer from store ID
public string Number { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string Country { get; set; }
public string MobilePhone { get; set; }
public System.DateTime DOB { get; set; }
public string Phone { get; set; }
//public User Credentials { get; set; }
public string DeviceToken { get; set; }
//public CustomerCard Details { get; set; }
public string Gender { get; set; }
public bool IsError { get; set; }
public string ErrorMessage { get; set; }
//Public Property StoreNumber As Integer
public string Token { get; set; }
public bool Authenticated { get; set; }
public string SecretKey { get; set; }
}
ApiRequestクラス:
private void button1_Click(object sender, EventArgs e)
{
Customer cust = new Customer();
InsertUpdateCustomer(cust, "http://Example.com");
}
public static Customer InsertUpdateCustomer(Customer MyCustomer, string ServerAddress)
{
//Dim PostData As New CardInfo With {.CardNumber = CardNumber, .Reference = Reference, .SaleDate = DateTime.Now, .SaleTotalAmount = Amount, .StoreNumber = StoreNumber, .TransactionTypeID = Transaction}
//Customer Res = new Customer();
string webAddr = ServerAddress + "/api/Customer/Insert";
WebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("X-Giftworx-App", "Posworx");
httpWebRequest.Timeout = 4000;
MyCustomer.Name = "Janice";
MyCustomer.Surname = "Alexander";
MyCustomer.Email = "[email protected]";
MyCustomer.MobilePhone = "0314011828";
MyCustomer.Gender = "Female";
MyCustomer.DOB = DateTime.Now;
MyCustomer.Token = "wMq0cZ4iN7uOnJdrSdYITQcWHQ9VYgiLCosN7Rj9MSdqmZKSTuHCb08jeO/wlp3bCoK/sbEwwvjlZUeQdj8p5w==";
MyCustomer.SecretKey = "jdghe45";
MyCustomer.Authenticated = true;
try
{
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string jsonstring;
MemoryStream stream1 = new MemoryStream();
//Serialize the Record object to a memory stream using DataContractJsonSerializer.
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Customer));
ser.WriteObject(stream1, MyCustomer);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
jsonstring = sr.ReadToEnd();
Debug.WriteLine("JSON form of Insert Loyaltyworx Update Customer object: ");
Debug.WriteLine(JObject.Parse(jsonstring));
streamWriter.Write(jsonstring);
streamWriter.Flush();
}
}
catch (Exception ex)
{
return null;
}
try
{
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string result = streamReader.ReadToEnd();
Console.WriteLine(JObject.Parse(result));
Customer MyResult = JsonConvert.DeserializeObject<Customer>(result);
Debug.WriteLine(JObject.Parse(result));
return MyResult;
}
}
else
{
return null;
}
}
catch (Exception ex)
{
return null;
}
}
サーバーサイド(Webサーバー)をデバッグするとき、401を返す前に何が起こるのですか?呼び出す(サーバー側)メソッドのソースコードを含めてください... – mjwills
応答してくれてありがとう、私はnullableフィールドを欠落していたことに気付きました。 :) –