Xamarin:NSMutableUrlRequest 和 NSUrlRequest 与 POST 方法和 headers 和数据?
Xamarin: NSMutableUrlRequest and NSUrlRequest with POST method and headers and data?
我有一个 Xamarin 项目,我可以在没有 header 的情况下对类似 "reqres.in" API 的东西进行 GET 调用,例如:
public Task<string> GetData()
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
NSUrl url = new NSUrl("https://reqres.in/api/users?page=2");
NSUrlRequest request = new NSUrlRequest(url);
NSUrlSession session = null;
NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
//config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover; //for some reason this does not work!!
myConfig.MultipathServiceType = (NSUrlSessionMultipathServiceType)2; //but this works!!
session = NSUrlSession.FromConfiguration(myConfig);
NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) => {
//Console.WriteLine(data);
//tell the TaskCompletionSource that we are done here:
tcs.TrySetResult(data.ToString());
});
task.Resume();
return tcs.Task;
}
但我尝试了很多变体,但无法使用类似技术与 header 和 body 进行 POST 调用。我尝试过:
public Task<string> GetDataFromInternet()
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
NSUrl url = NSUrl.FromString("https://content.dropboxapi.com/2/files/download");
NSUrlRequest req = new NSUrlRequest(url);
var authToken = "Bearer my-access-token";
if (!string.IsNullOrEmpty(authToken))
{
NSMutableUrlRequest mutableRequest = new NSMutableUrlRequest(url);
try
{
NSMutableDictionary dic = new NSMutableDictionary();
dic.Add(new NSString("Authorization"), new NSString(authToken));
dic.Add(new NSString("Dropbox-API-Arg"), new NSString("{\"path\":\"/path/to/my/file.json\"}"));
mutableRequest.Headers = dic;
NSData body = "{\"name\":\"morpheus\", \"job\": \"leader\"}";
mutableRequest.Body = body;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
mutableRequest.HttpMethod = "POST";
req = (NSUrlRequest)mutableRequest.Copy();
}
NSUrlSession session = null;
NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
myConfig.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;
session = NSUrlSession.FromConfiguration(myConfig);
NSUrlSessionTask task = session.CreateDataTask(req, (data, response, error) =>
{
//Console.WriteLine(data);
//tell the TaskCompletionSource that we are done here:
tcs.TrySetResult(data.ToString());
});
task.Resume();
return tcs.Task;
}
但这总是returns"Could not connect to server"。我已经验证了这个 URL 与我的 "access token" 和其他 header 和 body 数据工作正常。
请告诉我如何才能完成这项工作。谢谢。
最终修复:
我的代码和'Junior Jiang - MSFT'在solution中写的代码几乎一模一样。两者都有效,但我发现它们不起作用的真正原因是 iOS 捆绑签名选项中缺少 'Entitlements.plist'。由于 'Multipath TCP' 权利在此文件中,我发现仅选择该选项无济于事。相反,我们必须专门将其包含在 'Custom Entitlement' 部分中,如下所示:
这里是关于POST请求的示例代码:
NSUrl nsurl = NSUrl.FromString("Your url adress");//http://www.apple.com/
NSMutableUrlRequest request = new NSMutableUrlRequest(nsurl);
request.HttpMethod = "POST";
NSMutableDictionary dic = new NSMutableDictionary();
dic.Add(new NSString("Content-Type"), new NSString("application/json"));
request.Headers = dic; // add Headers
request.Body = NSData.FromString("{\"version\":\"v1\", \"cityid\": \"101010100\"}"); //add body
NSUrlSession session = NSUrlSession.SharedSession;
NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) =>
{
Console.WriteLine("---"+response);
Console.WriteLine("---"+ data);
Console.WriteLine("---"+ error);
});
task.Resume();
我有一个 Xamarin 项目,我可以在没有 header 的情况下对类似 "reqres.in" API 的东西进行 GET 调用,例如:
public Task<string> GetData()
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
NSUrl url = new NSUrl("https://reqres.in/api/users?page=2");
NSUrlRequest request = new NSUrlRequest(url);
NSUrlSession session = null;
NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
//config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover; //for some reason this does not work!!
myConfig.MultipathServiceType = (NSUrlSessionMultipathServiceType)2; //but this works!!
session = NSUrlSession.FromConfiguration(myConfig);
NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) => {
//Console.WriteLine(data);
//tell the TaskCompletionSource that we are done here:
tcs.TrySetResult(data.ToString());
});
task.Resume();
return tcs.Task;
}
但我尝试了很多变体,但无法使用类似技术与 header 和 body 进行 POST 调用。我尝试过:
public Task<string> GetDataFromInternet()
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
NSUrl url = NSUrl.FromString("https://content.dropboxapi.com/2/files/download");
NSUrlRequest req = new NSUrlRequest(url);
var authToken = "Bearer my-access-token";
if (!string.IsNullOrEmpty(authToken))
{
NSMutableUrlRequest mutableRequest = new NSMutableUrlRequest(url);
try
{
NSMutableDictionary dic = new NSMutableDictionary();
dic.Add(new NSString("Authorization"), new NSString(authToken));
dic.Add(new NSString("Dropbox-API-Arg"), new NSString("{\"path\":\"/path/to/my/file.json\"}"));
mutableRequest.Headers = dic;
NSData body = "{\"name\":\"morpheus\", \"job\": \"leader\"}";
mutableRequest.Body = body;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
mutableRequest.HttpMethod = "POST";
req = (NSUrlRequest)mutableRequest.Copy();
}
NSUrlSession session = null;
NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
myConfig.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;
session = NSUrlSession.FromConfiguration(myConfig);
NSUrlSessionTask task = session.CreateDataTask(req, (data, response, error) =>
{
//Console.WriteLine(data);
//tell the TaskCompletionSource that we are done here:
tcs.TrySetResult(data.ToString());
});
task.Resume();
return tcs.Task;
}
但这总是returns"Could not connect to server"。我已经验证了这个 URL 与我的 "access token" 和其他 header 和 body 数据工作正常。
请告诉我如何才能完成这项工作。谢谢。
最终修复:
我的代码和'Junior Jiang - MSFT'在solution中写的代码几乎一模一样。两者都有效,但我发现它们不起作用的真正原因是 iOS 捆绑签名选项中缺少 'Entitlements.plist'。由于 'Multipath TCP' 权利在此文件中,我发现仅选择该选项无济于事。相反,我们必须专门将其包含在 'Custom Entitlement' 部分中,如下所示:
这里是关于POST请求的示例代码:
NSUrl nsurl = NSUrl.FromString("Your url adress");//http://www.apple.com/
NSMutableUrlRequest request = new NSMutableUrlRequest(nsurl);
request.HttpMethod = "POST";
NSMutableDictionary dic = new NSMutableDictionary();
dic.Add(new NSString("Content-Type"), new NSString("application/json"));
request.Headers = dic; // add Headers
request.Body = NSData.FromString("{\"version\":\"v1\", \"cityid\": \"101010100\"}"); //add body
NSUrlSession session = NSUrlSession.SharedSession;
NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) =>
{
Console.WriteLine("---"+response);
Console.WriteLine("---"+ data);
Console.WriteLine("---"+ error);
});
task.Resume();