gpt4 book ai didi

java - 将 php 转换为 Java 代码的麻烦

转载 作者:行者123 更新时间:2023-12-01 15:11:04 25 4
gpt4 key购买 nike

我正在尝试用 java 编写以下 php 代码部分。我将提供php代码和java代码。我想要帮助的是a)我是否走在正确的轨道上,b)带有“请在此提供帮助”评论的行,我不确定如何在java中执行此操作。这一行是 header("Location: ".$strCitiRedirectURL.$content."");

提前谢谢您。

PHP 代码:

$req =& new HTTP_Request($strCitiLoginURL);
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addPostData("instusername", $strInstUsername);
$req->addPostData("institution", $strInstitution);
$req->addPostData("key", $strInstitutionKey);
$req->addPostData("type", "returning");

$response = $req->sendRequest();

if(isset($_GET['showDebug'])){
print $req->_buildRequest();
}

if (PEAR::isError($response)) {
$content = $response->getMessage();
} else {
$content = $req->getResponseBody();
}

/* Check for 44 Character UUID */
if (preg_match($pattern,$content)){
print 'Success';
ob_start();
header("Location: ".$strCitiRedirectURL.$content."");
ob_flush();
/* No UUID. Login to CITI failed. We may need a new user */
}elseif ($content == "- error: learner not affiliated with institution, add learner or provide username and password"){

// Resubmit as a new user
/* Package data up to post to CITI */
$req =& new HTTP_Request($strCitiLoginURL);
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addPostData("instusername", $strInstUsername);
$req->addPostData("institution", $strInstitution);
$req->addPostData("key", $strInstitutionKey);
$req->addPostData("type", "new");
$req->addPostData("first", $strFirst);
$req->addPostData("last", $strLast);
$req->addPostData("email", $strEmail);

$response = $req->sendRequest();

if(isset($_GET['showDebug'])){
print $req->_buildRequest();
}

if (PEAR::isError($response)) {
$content = $response->getMessage();
} else {
$content = $req->getResponseBody();
}

/* Check for 44 Character UUID */
if (preg_match($pattern,$content)){
print 'Success';
ob_start();
/*PLEASE HELP ON THIS LINE*/ header("Location: ".$strCitiRedirectURL.$content."");
ob_flush();
}else{
$errMsg = $errMsg.' <li>CITI Error Returned: '.$content.'.</li>';
}

java代码

//****CITI CONFIGURATION****
final String pattern = "([0-9A-\\-]{44})";
final String CitiRedirectUrl = "https://www.citiprogram.org/members/mainmenu.asp?strKeyID=";
final String CitiLoginUrl = "http://www.citiprogram.org/remoteloginII.asp";
//****END CITI CONFIGURATION****

try {
// Construct data
String data = URLEncoder.encode("instusername", "UTF-8") + "=" + URLEncoder.encode(c_form.getCan(), "UTF-8");
data += "&" + URLEncoder.encode("institution", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
data += "&" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode("returning", "UTF-8");

// Send data
URL url = new URL("http://www.citiprogram.org/remoteloginII.asp");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
if (pregMatch(pattern, line)) {
//Do the header part from the php code
} else if (line.equals("- error: learner not affiliated with institution, add learner or provide username and password")) {
// Resubmit as a new user
/* Package data up to post to CITI */

// Construct data
String newdata = URLEncoder.encode("instusername", "UTF-8") + "=" + URLEncoder.encode(c_form.getCan(), "UTF-8");
newdata += "&" + URLEncoder.encode("institution", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
newdata += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
newdata += "&" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode("returning", "UTF-8");

// Send data
OutputStreamWriter newwr = new OutputStreamWriter(conn.getOutputStream());
newwr.write(data);
newwr.flush();

// Get the response
BufferedReader newrd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String newline;
while ((newline = newrd.readLine()) != null) {
System.out.println(newline);
if (pregMatch(pattern, newline)) {
} else {
//Print error message
}
}
}
}
wr.close();
rd.close();
} catch (Exception e) {
}

//Check for 44 character UUID
public static boolean pregMatch(String pattern, String content) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(content);
boolean b = m.matches();
return b;
}

最佳答案

我相信

header("Location: ".$strCitiRedirectURL.$content."");

在 PHP 中与 Java 中的以下内容相同(使用您的 wr 对象):

wr.sendRedirect("http://path.to.redirect/");

您也可以转发请求,但我有一种感觉,您只是希望客户端重定向到 citirewards 或其他什么,在这种情况下,sendRedirect 是解决方案。

编辑:来源-http://docs.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletResponse.html#sendRedirect(java.lang.String )

关于java - 将 php 转换为 Java 代码的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12357115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com