简体   繁体   English

iOS上的Http请求

[英]Http requests on iOS

So i am trying to make an http request using ASIHTTPRequest class , to send a json file to my server. 所以我试图使用ASIHTTPRequest类发出一个http请求,将json文件发送到我的服务器。 I was given an android code which is functional and was asked to convert it to iOS code. 我得到了一个功能强大的Android代码,并被要求将其转换为iOS代码。 I cant understand how to add headers on my request.The android code looks something like this : 我无法理解如何在我的请求上添加标题。安卓代码看起来像这样:

private static final String CONTENT_TYPE = "Content-Type";
private static final String JSON_CONTENT_TYPE = "application/json; charset=utf-8";

and then : 接着 :

request.addHeader(CONTENT_TYPE, JSON_CONTENT_TYPE);

On iOS i figured out that the code is something like this : 在iOS上,我发现代码是这样的:

[request addRequestHeader:@"Content-Type" value:@"Content-Type"];
[request addRequestHeader:@"Json-Content-Type" value:@"application/json; charset=utf-8"];

BUT i got a bit confused , cause on android the request seems to be paired and on iOS the requestHeaders are individually stated. 但我有点困惑,因为在Android上请求似乎是配对的,在iOS上requestHeaders是单独声明的。 Is this a problem? 这是一个问题吗? Should they be paired too on iOS. 它们是否应该在iOS上配对。 Am i missing something here? 我错过了什么吗? Thank you for reading my post :D 感谢您阅读我的帖子:D

What you want is 你想要的是什么

[request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];

in the android example, they are just declaring two variables for the header values, they could had simply done something like 在android示例中,他们只是为标题值声明了两个变量,他们可以简单地完成类似的操作

request.addHeader("Content-Type", "application/json; charset=utf-8");

which would have had the same effect, so you just need the one line i mentioned above. 这会有相同的效果,所以你只需要我上面提到的那一行。

also: ASIHTTPREQUEST does not get developed any more (see headline here) , so consider using another library for something like this, like AFNetworking 另外: ASIHTTPREQUEST不再开发了(请参阅此处的标题) ,因此请考虑使用其他库来实现此类目标,例如AFNetworking

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM