我需要从java提交一个aspx页面.我正在使用HTTp Client以及HttpUrlConnection这样做.调用页面很简单,但我需要设置一个单选按钮以检查状态,然后将输入字段的值设置为我要搜索的内容并发布页面.
我在HttpUrlConnection上使用了post requestmethod,并尝试使用值作为编码字符串设置输入字段的值 - 不知道这是否是正确的方法.另外我不知道如何设置单选按钮状态进行检查
所以你们可以帮助我如何完成这项任务.
任何帮助将受到高度赞赏
谢谢
马诺
我想通过URLconnection检查下载文件的进度.是否可以或应该使用其他库?这是我的urlconnection函数:
public static String sendPostRequest(String httpURL, String data) throws UnsupportedEncodingException, MalformedURLException, IOException {
URL url = new URL(httpURL);
URLConnection conn = url.openConnection();
//conn.addRequestProperty("Content-Type", "text/html; charset=iso-8859-2");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));
String line, all = "";
while ((line = rd.readLine()) != null) {
all = all + line;
}
wr.close();
rd.close();
return all;
}
Run Code Online (Sandbox Code Playgroud)
我知道整个文件都是在这行(或worng)下载的?:
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));
Run Code Online (Sandbox Code Playgroud)
那么可以在这段代码中执行此操作吗?
我需要从另一个servlet调用servlet POST方法并在servlets参数中传递一个blob.这是可能的,如果是这样的话怎么办呢.PS:我不能使用Apache HttpClient
我在JAR中有一个我无法改变的第三方servlet.我已经扩展了该servlet并且正常使用它,因为应该使用servlet,客户端发出一个调用我的servlet的HTTP请求.
但是现在客户端想要一个自动服务,也就是说,我需要从servlet所在的同一个webapp对该第三方servlet做一些请求.
我查看了第三方servlet代码,但我没有找到绕过servlet的地方因为HttpServletRequest和HttpServletResponse对象从方法传递给方法......基本上我似乎需要重新实现所有第三方派对代码.
我找到的解决方案,但不满足我:
使用HttpURLConnection从URL调用servlet:我的常识是从url调用第三方servlet不是最好的方法,除了增加的开销之外,我不想暴露第三方servlet.从URL调用我的servlet也会带来会话和其他事情的问题.
直接调用doGet:这似乎是不可能的,因为没有HttpServletRequest和HttpServletResponse的实现.
使用jMock或类似的东西:尚未探索此解决方案,但在真实环境中使用测试驱动的库会出错.
任何人都知道如何与第三方servlet交互?
编辑:
由于我的英语不是很好,我发现自己很难解释,这里有一个原理图,试图更好地解释

编辑2:在与第三方制造商会面后,他们提出隔离我需要避免调用servlet的方法.如果你没有同样的运气,我确实检查了gigadot和BalusC的答案.
我有一个java servlet,它接受用户上传到我的web应用程序的图像.
我有另一台服务器(运行php),它将托管所有图像.如何从我的jsp服务器获取图像到我的php服务器?流程将是这样的:
public class ServletImgUpload extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// get image user submitted
// try sending it to my php server now
// return success or failure message back to user
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个看起来像这样的 HTML 表单:
<form name="form1" method="post" action="/confirm.asp">
<input type="text" name="data1" size="20" value=""><br>
<input type="text" name="data2" size="20" value=""><br>
<input type="Submit" name=submit value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
我想用Java来传递数据data1和data2和读取表单提交时下面的页面。由于这是一个 method=post,我不能使用http://somesite.com/confirm.asp?data1=foo&data2=foo.
可以帮忙吗?
我有非常简单的JSF bean,如下所示:
import org.jboss.seam.annotations.Name;
@Name(Sample.NAME)
public class Sample {
public static final String NAME="df";
private String text = "text-test";
public void sampleM(){
System.out.println("Test: "+text);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Run Code Online (Sandbox Code Playgroud)
和JSF表单连接这个组件:
<h:form id="sampleForm">
<h:commandButton id="sampleButton" action="#{df.sampleM()}" value="ok" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
现在,我想以编程方式将POST请求发送到此表单.
根据我的调查,这里的关键是POST参数.正确选择可以得到正确的结果(字符串'测试:文本测试'打印在serwer的控制台上).
所以问题是:我应该如何选择正确的POST数据?
上面显示的JSF表单生成此HTML表单:
<form id="sampleForm" name="sampleForm" method="post" action="/pages/main/main.smnet" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="sampleForm" value="sampleForm" />
<input id="sampleForm:sampleButton" type="submit" name="sampleForm:sampleButton" value="ok" />
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="j_id65" autocomplete="off" /> …Run Code Online (Sandbox Code Playgroud) 我正在尝试从此目录下载所有文件.但是,我只能将它作为一个文件下载.我能做什么?我试图搜索这个问题,这让人感到困惑,人们开始建议使用httpclients.感谢您的帮助,这是我的代码到目前为止.有人建议我使用输入流来获取目录中的所有文件.那么那会进入阵列吗?我在http://docs.oracle.com/javase/tutorial/networking/urls/尝试了这个教程,但它没有帮助我理解.
//ProgressBar/Install
String URL_LOCATION = "http://www.futureretrogaming.tk/gamefiles/ProfessorPhys/";
String LOCAL_FILE = filelocation.getText() + "\\ProfessorPhys\\";
try {
java.net.URL url = new URL(URL_LOCATION);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/4.76");
//URLConnection connection = url.openConnection();
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
int available = stream.available();
byte b[]= new byte[available];
stream.read(b);
File file = new File(LOCAL_FILE);
OutputStream out = new FileOutputStream(file);
out.write(b);
} catch (Exception e) {
System.err.println(e);
}
Run Code Online (Sandbox Code Playgroud)
我还发现这个代码将返回要下载的文件列表.有人可以帮我合并这两个代码吗?
public class GetAllFilesInDirectory {
public static void main(String[] args) throws IOException {
File dir = …Run Code Online (Sandbox Code Playgroud) 我使用以下代码将JSON请求发送到我的Web服务,对于错误的请求,我返回了400消息代码以及有效负载中的详细JSON错误响应。
我不明白在使用时如何在客户端上检索此有效负载HttpURLConnection,因为它会立即引发IOException和连接InputStream是null。
HttpURLConnection connection = this.connect(url);
connection.setRequestMethod(method);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(jsonMessage);
InputStream is = null;
try {
is = connection.getInputStream(); // IOException is thrown, how can I still get payload??
} catch (Exception e) {
}
String response = getStringFromInputStream(is);
Run Code Online (Sandbox Code Playgroud)
也尝试使用getErrorStream(),但这包含Apache Tomcat的默认错误页面,而不是我在例如使用可视REST API客户端时看到的有效负载。