2015-06-18 67 views
0

我有以下代碼發送發佈數據到服務器,但服務器沒有檢測到請求上的任何發佈數據。客戶端代碼:http.newRequest不發送發佈數據

cookieJar, _ := cookiejar.New(nil) 
     client := &http.Client{ 
      Jar: cookieJar, 
     } 
     postUrl := os.Args[1] 
     username := os.Args[2] 
     password := os.Args[3] 
     data := url.Values{} 
     data.Set("username", username) 
     data.Add("password", password) 
     data.Add("remember", "false") 

     r, _ := http.NewRequest("POST", postUrl, bytes.NewBufferString(data.Encode())) 
     resp, _ := client.Do(r) 
     defer resp.Body.Close() 
     body, err := ioutil.ReadAll(resp.Body) 
     fmt.Printf("%s\n", string(body)) 

Server代碼:

if(isset($_POST['username'], $_POST['password'], $_POST['remember'])){ 
    echo $username = md5(($_POST['username'])); 
    echo $password_original = $_POST['password']; 
    echo $password = md5(($_POST['password'])); 
    echo $remember = $_POST['remember']; 
} 

回答

2

您發佈的數據作爲請求體形式沒有內容類型。你要麼需要把值請求查詢:

r.URL.RawQuery = data.Encode() 

或更改內容類型爲「application/X WWW的形式,進行了urlencoded」:

r.Header.Add("Content-Type", "application/x-www-form-urlencoded")