用JAVA模拟POST发送数据

xydragon 2005-08-05 05:33:21
现在要做一个独立的程序,要求与另一个软件提供商进行数据交换,对方要求交换程序按以下要求来做:
1、POST 方式传递数据
2、输入方式必须是: multipart/form-data
3、必须用 requestxml 作为输入变量
4、传递的数据必须是 XML 格式
有哪位可以提供思路,或者代码参考.
谢谢!
...全文
1883 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mygia 2006-04-21
  • 打赏
  • 举报
回复
好精彩,收藏!
phoenixandlinda 2005-08-07
  • 打赏
  • 举报
回复
关注
xydragon 2005-08-07
  • 打赏
  • 举报
回复
在 xiaohuasz() 发的内容中有这样一句conn.setRequestProperty("Referer","http://203.238.87.206/send2.asp"); 有两个问题,1、不太明白它的意思;2、第二参数是起什么作用的,它与 URL servlet = new URL("http://61.74.66.185/p/send_notip.asp"); 这一句中的URL啷个不一样哩?
请大家继续讨论!
谢谢了
simon0512 2005-08-06
  • 打赏
  • 举报
回复
up
gsen 2005-08-06
  • 打赏
  • 举报
回复
up
xiaohuasz 2005-08-06
  • 打赏
  • 举报
回复
1.可以设置
2.输出方式需遵循一定规则
可参考http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.2

下列代码摘自网上:
/**
*
* @author guisset
*/


import java.net.*;
import java.io.*;

public class EssaiPostURLConnection {

private static void writeParam(String name, String value,DataOutputStream out, String boundary) {
try {
out.writeBytes("content-disposition: form-data; name=\"" + name+ "\"\r\n\r\n");
out.writeBytes(value);
out.writeBytes("\r\n" + "--" + boundary + "\r\n");
} catch (Exception e) { System.out.println(e.toString()); }
}

private static void writeFile(String name, String filePath,DataOutputStream out, String boundary) {
try {
out.writeBytes("content-disposition: form-data; name=\"" + name +"\"; filename=\""
+ filePath + "\"\r\n");
out.writeBytes("content-type: application/octet-stream" +"\r\n");
FileInputStream fis = new FileInputStream(filePath);
while (true) {
synchronized (buffer) {
int amountRead = fis.read(buffer);
if (amountRead == -1) {
break;
}
out.write(buffer, 0, amountRead);
}
}
fis.close();
out.writeBytes("\r\n" + "--" + boundary + "\r\n");
} catch (Exception e) { System.out.println(e.toString());
}
}

static final int BUFF_SIZE = 1024;
static final byte[] buffer = new byte[BUFF_SIZE];

/** Creates a new instance of EssaiPostURLConnection */
public EssaiPostURLConnection() {
}

/**
* @param args the command line arguments
*/
public static void main (String args[]) {
try {
URL servlet = new URL("http://61.74.66.185/p/send_notip.asp");
URLConnection conn=servlet.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
String boundary = "---------------------------7d226f700d0";
conn.setRequestProperty("Content-type","multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("Referer","http://203.238.87.206/send2.asp");
conn.setRequestProperty("Host", "61.74.66.185");
conn.setRequestProperty("Cache-Control", "no-cache");

DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes("--" + boundary + "\r\n");

writeParam("u1", "k77", out, boundary);
writeParam("j1", "", out, boundary);
writeParam("o1", "", out, boundary);
writeParam("d1", "", out, boundary);
writeFile("i11", "C:\\11.jpg", out, boundary);
writeParam("i1", "", out, boundary);
writeParam("s1", "", out, boundary);
writeParam("i12", "", out, boundary);
writeParam("c13", "", out, boundary);

out.flush();
out.close();

InputStream stream = conn.getInputStream();
BufferedInputStream in = new BufferedInputStream(stream);
int i =0;
int j=0;
while ((i = in.read()) != -1) {
System.out.write(i);
System.out.println("testing");
}
System.out.write(j);
in.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
ideal2004 2005-08-06
  • 打赏
  • 举报
回复
HttpURLConnection conn = new HttpURLConnectino(URL url)
方法根本不能用啊,是抽象的?怎么办呢?
xydragon 2005-08-06
  • 打赏
  • 举报
回复
谢谢各位的意见,我的做法跟楼上的 jFresH_MaN 和 xiaohuasz() 的大同小异,现在出现的问题是,
1、输入方式必须是: multipart/form-data ,在程序中能不能够conn.setRequestProperty("Content-Type", "multipart/form-data");这样设置????
2、必须用 requestxml 作为输入变量,考虑可能是另一个软件提供商用这个变量来接收值,那么在程序中能不能用后边的这些语句? OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream(), "8859_1"); out.write("requestxml="+strXML);
请各位帮着看一下。
joincsdn 2005-08-05
  • 打赏
  • 举报
回复
使用Socket应该也能实现楼主所说的要求
xiaohuasz 2005-08-05
  • 打赏
  • 举报
回复
public void PostXml(String strXML)
{
HttpURLConnection conn = ...
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\"");
conn.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"UTF8");
out.write(strXML);
out.close();
}
laughsmile 2005-08-05
  • 打赏
  • 举报
回复
服务器:
1.jsp
<body>
<form name="_ctl0" method="post" action="TestFileManager.aspx" id="_ctl0" enctype="multipart/form-data">
<input type="hidden" name="__VIEWSTATE" value="dDwyNTIzNjA5NDU7Oz7rsE3eBYzQHDVtl+aTn96MvQW6PQ==" />

<p>
<input name="uploadfile1" id="uploadfile1" type="file" size="49" />
<input type="submit" name="Button1" value="?" id="Button1" />
</p>
<p>
<span id="Label1" style="width:459px;"></span>
</p>
<!-- Insert content here -->
</form>
</body>
客户端:
首先创建一个到服务器http的请求
HttpRequest request = new HttpRequest("http://服务器/1.jsp");
第一次使用的是GET方式
request.setMethod("GET");
紧接着进行一些请求的属性设置
request.setRequestHeader("Cache-Control", "no-cache");
这里保持连接,因为后面还要发送数据到服务器呢
request.setRequestHeader("Connection", "Keep-Alive");
下面是一些无关紧要的属性设置了。
request.setRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
request.setRequestHeader("Accept-Encoding", "gzip, deflate");
request.setRequestHeader("Accept-Language", "en-au");
request.setRequestHeader("Referer", "http://服务器/1.jsp");
request.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3215; .NET CLR 1.0.3705)");

构造好了连接请求,然后连接
request.connect();

紧接着提取Cookie值,在后文的post中可以用到。
String strCookie = request.getResponseHeader("Set-Cookie");
strCookie = strCookie.substring(0,strCookie.indexOf(";"));

下面通过循环查找,提取__VIEWSTATE的值
for ( int i = 0; i < nlist.getLength(); i++) {
node = nlist.item(i);
strName = getNodeAttributeValue(node,"name");
if ( strName.equals("__VIEWSTATE") ) {
strValue = getNodeAttributeValue(node,"value");
break;
}
}

往服务器组织发送数据
DataOutputStream dos = new DataOutputStream(request.getOutputStream());
dos.writeBytes("-----------------------------"+strBoundary);//这是每个要被发送数据间的间隔
dos.writeBytes("\r\nContent-Disposition: form-data; name=\"__VIEWSTATE\"");
dos.writeBytes("\r\n\r\n"+strValue);
dos.writeBytes("\r\n-----------------------------"+strBoundary);
这里面是发送文件的部分
dos.writeBytes("\r\nContent-Disposition: form-data; name=\"uploadfile1\"; filename=\"" + strFileName + "\"");
dos.writeBytes("\r\nContent-Type: text/xml");
dos.writeBytes("\r\n\r\n");
dos.writeBytes(new String(data));
dos.writeBytes("\r\n-----------------------------"+strBoundary);
dos.writeBytes("\r\nContent-Disposition: form-data; name=\"Button1\"");
dos.writeBytes("\r\n上传");
dos.writeBytes("\r\n-----------------------------"+strBoundary+"--");
dos.writeBytes("\r\n");
dos.close();
jyh149129 2005-08-05
  • 打赏
  • 举报
回复
Up!
jFresH_MaN 2005-08-05
  • 打赏
  • 举报
回复
// Using java.net.URL and
//java.net.URLConnection
URL url = new
URL("http://www.126.com/index_t.htm");
URLConnection connection = url.openConnection();
然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做:

...
connection.setDoOutput(true);
最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如:

...
OutputStreamWriter out = new
OutputStreamWriter(uc.getOutputStream(), "8859_1");
out.write("username=bob&password="+password+"");
// remember to clean up
out.flush();
out.close();
这样就可以发送一个看起来象这样的POST:

POST www.126.net/index.html HTTP 1.0
ACCEPT: text/plain
Content-type: application/x-www-form-urlencoded
Content-length: 99
username=bob
password=someword
一旦发送成功,用以下方法就可以得到服务器的回应:

connection.getInputStream();


上面是发送字符串的,其实你要发送xml文件,只要把xml文件的内容保存成字符串发送过去就可以了


但是如果实在要使用multipart/form-data
那就要使用URLConnection的setContentHandlerFactory(ContentHandlerFactory fac)方法
然后写一个类实现Interface ContentHandlerFactory
这样就可以直接发送XML这个Object了

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧