在Go中使用net / http进行身份验证无效

时间:2015-12-23 11:11:49

标签: http authentication go

我有一个问题,我有一台服务器登录到另一台服务器并获取一些数据。 登录在NodeJS中工作如下:

var data = {
    login_act: "username",
    login_pwd: "password"
};
request.post({url: 'https://example.com', form: data}, callback())`

和Java一样:

try {
    URL mUrl = new URL("https://example.com");

    HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
    connection.setDoOutput(true);
    connection.setInstanceFollowRedirects(false);

    String urlParameters  = "login_act=" + username + "&login_pwd=" + password;
    byte[] postData       = urlParameters.getBytes("UTF-8");

    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    try( DataOutputStream wr = new DataOutputStream( connection.getOutputStream())) {
        wr.write( postData );
    }
}

我也做了相同情况的C ++实现:

QNetworkAccessManager *vManager = new QNetworkAccessManager;
QNetworkRequest req;
req.setUrl(QUrl("https://example.com"));
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QByteArray postData;
postData.append("login_act=" + username + "&");
postData.append("login_pwd=" + password + "");

connect(vManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(authFinished(QNetworkReply*)));
QNetworkReply *reply = vManager->post(req, postData);
reply->ignoreSslErrors();

但是当我尝试在Go中做同样的事情时,它拒绝登录:

resp, error := http.PostForm("https://example.com", url.Values{"login_act": {"username"}, "login_pwd": {"password"}})

网站的构建方式是,当登录失败时,它只返回带有登录表单的标准站点,如果登录失败,则返回302状态码。 也许有人可以帮助我,我是Go编程语言的新手,也许我只是错过了一些重要的东西。 提前谢谢。

0 个答案:

没有答案