Java URL连接到php
这段代码在php中怎么写呢?
我应该用什么?卷曲? fsockopen ?以及实际发送到服务器的内容(outputString 是 post / get 及其变量名称)?
URL url = new URL(targetURL);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/xml");
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(outputString.getBytes("UTF-8"));
out.close();
conn.connect();
final int code = conn.getResponseCode();
final String contentType = conn.getContentType();
final StringBuffer responseText = new StringBuffer();
InputStreamReader in = new InputStreamReader(conn.getInputStream(),"UTF-8");
char[] msg = new char[2048];
int len;
while ((len = in.read(msg)) > 0) {
responseText.append(msg, 0, len);
}
感谢您的任何答复。
How to write this code in php?
What i should use? CURL? fsockopen ? and what is actually send to server (outputString is a post / get and what its variable name)?
URL url = new URL(targetURL);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/xml");
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(outputString.getBytes("UTF-8"));
out.close();
conn.connect();
final int code = conn.getResponseCode();
final String contentType = conn.getContentType();
final StringBuffer responseText = new StringBuffer();
InputStreamReader in = new InputStreamReader(conn.getInputStream(),"UTF-8");
char[] msg = new char[2048];
int len;
while ((len = in.read(msg)) > 0) {
responseText.append(msg, 0, len);
}
Thank you for any answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 cURL 帖子的基本示例...
进一步阅读 http: //www.php.net/manual/en/function.curl-exec.php 也有很好的例子。
发送 XML 的示例:
第三个是很好的衡量标准,只是为了说明另一种方法;
This is a basic example of a cURL post...
Further reading at http://www.php.net/manual/en/function.curl-exec.php has very good examples too.
An example for SENDING XML:
And a 3rd for good measure, just to illustrate an alternative approach;