html form 测试请求,HTML发表请求(登录表单)

博主在尝试使用Java实现类似于C#的登录POST请求时遇到问题,服务器返回登录页面而没有错误提示。他们已经收集了必要的安全令牌、cookie、用户名和密码等信息,并设置了请求头。在尝试了多种方法后,包括使用Apache HTTP Components,问题依然存在。博客内容涉及到网络请求、身份验证和跨平台代码转换。
摘要由CSDN通过智能技术生成

我需要登录到一个网站。我试图通过发送正确的POST请求来将其归档。我可以收集所需的数据(两个安全令牌和一个cookie),这似乎没有任何问题。但是最终的登录过程不起作用 - 但令人遗憾的是:我不知道在哪里可以找到问题,因为服务器只是简单地将我重定向到登录页面而没有任何提示。 这是我的方法的当前状态:HTML发表请求(登录表单)

URL url = new URL("SERVER");

Map params = new LinkedHashMap<>();

params.put("security_token", security_token);

params.put("login_ticket", login_ticket);

params.put("loginname", "USERNAME");

params.put("password", "PASSWORD");

params.put("login", "Login");

StringBuilder postData = new StringBuilder();

for (Map.Entry param : params.entrySet()) {

if (postData.length() != 0) postData.append('&');

postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));

postData.append('=');

postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));

}

byte[] postDataBytes = postData.toString().getBytes("UTF-8");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestProperty("Cookie", cookie);

conn.setInstanceFollowRedirects(true);

conn.setRequestMethod("POST");

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

conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));

conn.setDoOutput(true);

conn.getOutputStream().write(postDataBytes);

Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

Map> headerFields = conn.getHeaderFields();

我已经试图解决这个问题,C#。下面的代码工作正常,所以我只是试图将它“翻译”成java。

string cookie = "COOKIEVALUE";

request.Host = new Uri("SERVER");

//request.PostData.Add("Cookie", cookies[0]);

request.PostData.Add("loginname", "USERNAME");

request.PostData.Add("password", "PW");

request.PostData.Add("login_ticket","269ba20ad5a6f1a0219a3a333d3a5997");

request.PostData.Add("security_token", "ZHfyszNSuMrN8Xw80Pudka+5cZB20j+or+JWXCWzVPg=");

request.ContentType = "application/x-www-form-urlencoded";

try

{

request.SendRequest(cookie);

using (var response = request.GetResponse())

{

if (response == null)

throw new WebException();

using (var stream = response.GetResponseStream())

{

using (var streamReader = new StreamReader(stream))

{

string result = streamReader.ReadToEnd();

}

}

}

}

catch (WebException)

{

throw;

}

...

和sendRequest方法的代码

public void SendRequest(string cookieValue,byte[] buffer = null)

{

_webRequest = HttpWebRequest.CreateHttp(Host);

_webRequest.Method = "POST";

_webRequest.ContentType = ContentType;

_webRequest.CookieContainer = new CookieContainer();

var cookie = new Cookie("Seminar_Session", cookieValue);

cookie.Domain = Host.Host;

_webRequest.CookieContainer.Add(cookie);

string postString = "";

foreach (var item in PostData)

{

postString += item.Key + "=" + item.Value + "&";

}

if (postString.Length > 0)

postString = postString.Remove(postString.Length - 1, 1);

byte[] postBuffer = System.Text.Encoding.UTF8.GetBytes(postString);

_webRequest.ContentLength = postBuffer.Length;

if (buffer != null) _webRequest.ContentLength += buffer.Length;

using (var requestStream = _webRequest.GetRequestStream())

{

requestStream.Write(postBuffer, 0, postBuffer.Length);

if (buffer != null) requestStream.Write(buffer, 0, buffer.Length);

requestStream.Flush();

}

}

现在我试图让Java代码工作。有什么我明显做错了吗?很遗憾,我无法继续使用C#代码,现在需要Java中的工作解决方案。那么,我怎么才能解决这个问题与不工作的Java版本?

编辑:最后我用这些请求的apache组件。

String security_token;

String login_ticket;

//setup configuration

RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();

HttpClientContext context = HttpClientContext.create();

context.setCookieStore(_cookieStore);

//send request to get login data

Document source;

security_token = "TOKEN";

login_ticket = "TICKET";

HttpPost httppost = new HttpPost("https://www.studip.uni-goettingen.de/");

// Request parameters and other properties.

List params = new ArrayList(2);

params.add(new BasicNameValuePair("security_token", security_token));

params.add(new BasicNameValuePair("login_ticket", login_ticket));

params.add(new BasicNameValuePair("loginname", _credentials.getUserName()));

params.add(new BasicNameValuePair("password", _credentials.getPassword()));

httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute the login post request and get the response.

HttpResponse response = httpClient.execute(httppost, context);

HttpEntity entity = response.getEntity();

2016-04-21

FlashTek

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值