【问题标题】:Getting 405 error in POST NSMutableURLRequest在 POST NSMutableURLRequest 中出现 405 错误
【发布时间】:2013-11-24 08:08:03
【问题描述】:

我有一个与服务器通信的应用程序。我需要向服务器发送消息 ID。我正在使用 NSMutableURL 请求。但是,我在提交请求时收到 405 错误。

下面是代码

messageid=@"1234";
NSURL *aUrl = [NSURL     URLWithString:@"http://myexample.com:8080/Padua/rest/messages/messagetodelete"];
NSMutableURLRequest *deleterequest = [NSMutableURLRequest requestWithURL:aUrl
                                                                 cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                            timeoutInterval:60.0];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:deleterequest
                                                                  delegate:self];

[deleterequest setHTTPMethod:@"POST"];
[deleterequest setValue:[NSString stringWithFormat:@"%d", messageid.length] forHTTPHeaderField:@"Content-Length"];
[deleterequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *postString = messageid;
[deleterequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[connection start];

服务器端代码如下

@POST
@Consumes("text/plain")
@Path("/messagetodelete")
public void messageToDelete(String messageid){
    //code to delete the message in MongoDB
    System.out.println("R u here for iphone??");
}

客户端(iPhone)中的代码是否正确。 POST 使用纯文本。我相信这是我出错的地方。

谁能指导我如何实现它并使 NSMutableURLRequest 被服务器接受?

【问题讨论】:

    标签: ios macos http-post nsurlconnection nsmutableurlrequest


    【解决方案1】:

    存在以下潜在问题:

    • “删除”请求应使用 DELETE 方法。

    • header的定义
      [deleterequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

      错了。 MIME 类型application/x-www-form-urlencoded 没有参数,也没有字符集。服务器将忽略此参数。

      指定标头的正确方法是:

      [deleterequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
      

      并在应用百分比编码之前使用 UTF-8 作为参数字符串的字符编码。

    • 设置 Content-Length 标头时,您需要正确评估正文数据的长度以字节为单位。在您的代码中,您正在设置 string 的 UTF-16 代码单元的数量。因此,为了解决这个问题,您需要执行以下操作:

      NSString* postString = ...;
      NSString* postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
      [deleterequest setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
      [deleterequest setHTTPBody: postData];
      
    • 您需要确保参数字符串已正确编码。

    【讨论】:

    • 非常感谢 CouchDeveloper。你的回答对我帮助很大。第三点使代码工作。再次感谢。
    • @TimothyRajan 欢迎您 ;) 请您标记正确的答案吗?谢谢!
    【解决方案2】:

    如果您在服务器端接受 text/plain,那么您必须发送内容类型为 text/plain 的 POST 请求。此外,您缺少 Content-type 字符串中的分号。

    尝试改变

    [deleterequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    

    进入

    [deleterequest setValue:@"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    

    编辑:您还需要移动设置请求参数的部分在创建NSURLConnection之前,因为请求是深度复制的,创建连接后对请求的更改没有效果.

    【讨论】:

    • 感谢您的回复。仍然遇到同样的问题。我以前尝试过这段代码。无法找到问题所在。
    • 您的代码成就了我的一天。编辑完成了所有工作。我从来没有想过 NSURLConnection 的位置。不知何故,它从我的脑海中滑落。谢谢伙计。
    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 2017-09-09
    • 2021-04-27
    • 2021-10-16
    • 2017-06-14
    • 1970-01-01
    • 2019-01-24
    • 2014-12-04
    相关资源
    最近更新 更多