我已经使用Visual Studio 2019开发了一个ConsoleApp C#,可以在其中使用restsharp进行请求,并且运行良好。
这里有我用来发出请求的代码:
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace RetailerUrlStatusChecker
{
public class Program
{
static async Task Main(string[] args)
{
await ProcessRepositories();
}
private static async Task ProcessRepositories()
{
var client = new RestClient("https://api-developer.com/products");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
request.AddParameter("application/json", "{\"category\": \"all\"}", ParameterType.RequestBody);
var response2 = await client.ExecuteTaskAsync<List>(request);
List products= response2.Data;
//INSERT DATA INTO DB SQL SERVER
string connectionString = "Data Source=utente-pc\\SQLEXPRESS;Initial Catalog=MYDB;Persist Security Info=True;User ID=user;Password=sa";
SqlConnection connection = new SqlConnection(connectionString);
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO PRODUCT (code, name, description) VALUES (@code, @name, @description)", connection);
cmd.Parameters.AddWithValue("@code", products.code);
cmd.Parameters.AddWithValue("@name", products.name);
cmd.Parameters.AddWithValue("@description", products.description);
connection.Open();
cmd.ExecuteNonQuery();
}
catch
{
//Label4.Text = "uh oh";
}
}
}
public class List
{
public string code{ get; set; }
public string name{ get; set; }
public string description{ get; set; }
}
}
我需要在项目Web表单asp.net中实现此请求,我尝试编写相同的代码,但是它不起作用,我收到了响应null。
这是关于我的页面asp.net的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RestSharp;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
namespace WebApplication2
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterAsyncTask(new PageAsyncTask(GetGizmosSvcAsync));
}
public async Task GetGizmosSvcAsync()
{
var client = new RestClient("https://api-developer.com/products");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
request.AddParameter("application/json", "{\"category\": \"all\"}", ParameterType.RequestBody);
var response2 = await client.ExecuteTaskAsync<List>(request);
List products= response2.Data;
//INSERT DATA INTO DB SQL SERVER
string connectionString = "Data Source=utente-pc\\SQLEXPRESS;Initial Catalog=MYDB;Persist Security Info=True;User ID=user;Password=sa";
SqlConnection connection = new SqlConnection(connectionString);
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO PRODUCT (code, name, description) VALUES (@code, @name, @description)", connection);
cmd.Parameters.AddWithValue("@code", products.code);
cmd.Parameters.AddWithValue("@name", products.name);
cmd.Parameters.AddWithValue("@description", products.description);
connection.Open();
cmd.ExecuteNonQuery();
}
catch
{
//Label4.Text = "uh oh";
}
}
}
public class List
{
public string code{ get; set; }
public string name{ get; set; }
public string description{ get; set; }
}
}
我已经在页面asp.net的
我不明白为什么相同的代码在consoleapp c#上能很好地工作,但是在asp.net c#上却不能工作
感谢您的帮助!
响应方案是:
{
"code": {
"type": "string"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
}
}
[不知道端点响应的结构如何,很难为您提供帮助。但是基于名称(“https://api-developer.com/产品”),假设它返回产品集合。因此,尝试将var response2 = await client.ExecuteTaskAsync<List>(request);
语句修改为var response2 = await client.ExecuteTaskAsync<System.Collections.Generic.List<List>>(request);
<< [或根据您的下一条语句List products = response2.Data;
假设请求返回一些对象,该对象包含的数据是List对象的集合,如下所示:
public class RootObject()
{
public System.Collections.Generic.List<List> Data { get; set; }
}
如果是这种情况,请尝试将var response2 = await client.ExecuteTaskAsync<List>(request);
修改为var response2 = await client.ExecuteTaskAsync<RootObject>(request);
因此,您最终的
GetGizmosSvcAsync
方法如下所示:
public async Task GetGizmosSvcAsync() { var client = new RestClient("https://api-developer.com/products"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Accept", "application/json"); request.AddParameter("application/json", "{\"category\": \"all\"}", ParameterType.RequestBody); var response2 = await client.ExecuteTaskAsync<RootObject>(request); var products = response2.Data; //List products = response2.Data; //INSERT DATA INTO DB SQL SERVER string connectionString = "Data Source=utente-pc\\SQLEXPRESS;Initial Catalog=MYDB;Persist Security Info=True;User ID=user;Password=sa"; SqlConnection connection = new SqlConnection(connectionString); try { SqlCommand cmd = new SqlCommand("INSERT INTO PRODUCT (code, name, description) VALUES (@code, @name, @description)", connection); connection.Open(); foreach (var product in products) { cmd.Parameters.AddWithValue("@code", product.code); cmd.Parameters.AddWithValue("@name", product.name); cmd.Parameters.AddWithValue("@description", product.description); cmd.ExecuteNonQuery(); } } catch { //Label4.Text = "uh oh"; } }
现在,我想尝试在Web应用程序属性中设置client.encondig = {System.Text.UTF8Encoding.UTF8EncodingSealed},但我不知道如何更改此值...
与consoleapp和Web应用程序不同的请求仅是客户端。Encondif,我希望它可以解决我的问题。