Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么Golang将我的帖子请求视为GET请求?_Ios_Http_Go - Fatal编程技术网

Ios 为什么Golang将我的帖子请求视为GET请求?

Ios 为什么Golang将我的帖子请求视为GET请求?,ios,http,go,Ios,Http,Go,我的服务器代码如下: // golang type SomeHandler struct{} func (*SomeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { fmt.Println("method:", req.Method) fmt.Println("content length:", req.ContentLength) // read request body as []byte

我的服务器代码如下:

// golang
type SomeHandler struct{}

func (*SomeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    fmt.Println("method:", req.Method)
    fmt.Println("content length:", req.ContentLength)

    // read request body as []byte
    content, err := ioutil.ReadAll(req.Body)
    if err != nil {
        // do sth.
        return
    }

    // decode JSON
    // ...
}
客户端:

// Objective-C on iOS 6/7
// NSURL *myUrl = ...
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:myUrl];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

id obj = @{
             @"username" : @"myname",
             @"password" : @"mypwd",
          };
NSError *error;
NSData *inputJson = [NSJSONSerialization dataWithJSONObject:obj options:0 error:&error];
if (error) {
    // no error here
}
[req setHTTPBody:inputJson];

[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    if (error) {
        NSLog(@"ERROR: %@", error.localizedDescription);
        return;
    }

    NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
    NSString *outputText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"response (%d):\n%@", (int)resp.statusCode, outputText);
}
当我在iPhone模拟器中运行此iOS代码时,服务器端显示:

method: GET
content length: 0
更改我的客户端代码:
[req setHTTPMethod:@“HEAD”],服务器工作:

method: HEAD
content length: <some length>

我解决了这个问题!这么奇怪的行为。我不知道为什么会这样

我注册我的处理程序如下:

// addr := ...
r := mux.NewRouter()
r.Handle("/abc", new(SomeHandler))
http.Handle("/", r)
// here mux is github.com/gorilla/mux
// these lines of code do the same thing as wrote:
//   http.handle("/abc", new(SomeHandler))

err := http.ListenAndServe(addr, nil)
// ...
然后我的客户发出请求说

http://addr:9999//abc
但是正确的URL应该是

http://addr:9999/abc

调用
-stringByAppendingString:
方法时生成的冗余
/

您确定问题已经解决了吗?您是否尝试过不同的客户端/服务器组合?(例如,使用
curl
向服务器发送帖子,或让客户端尝试向netcat侦听套接字发送帖子)我刚刚解决了问题。真奇怪!!检查我的答案。看起来gorilla/mux会自动为非规范URL生成重定向响应。HTTP客户端通常不打算为了响应重定向而重做POST,这将解释您观察到的行为。是的,有一个新的请求发送到我的服务器。但这不是gorilla/mux造成的麻烦。直接使用net/http编写代码时也会发生同样的情况,比如
http.Handle(“/abc”,new(SomeHandler))
http://addr:9999/abc