dream12001 2015-12-23 11:11
浏览 17

Go中的net / http身份验证不起作用

I have a problem, I have a server that logs into another server and gets some data. The login works in NodeJS like this:

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

and in Java like this:

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 );
    }
}

I also made a C++ implementation of the same situation:

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();

But when I try to do the same in Go it refuses to log in:

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

The site is build in such a way that, when login fails it just returns the standard site with the login form and if login suceeds it returns a 302 statuscode. Maybe someone can help me I'm new to the go programming language and maybe I just miss something important. Thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭