请教一个问题

hdqqq 2012-06-13 09:09:48
用java准备请求一个url连接,url带查询参数。实现函数如下:

public static void open_url()
{
try {
String up_url = "http://192.168.1.60/upload/Default.aspx?id=1234567890";
java.net.URL url = new java.net.URL(up_url);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
}
catch (java.net.MalformedURLException e) {
e.printStackTrace();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
}
服务端用.net 实现,现在问题是服务端取不到id和提交的参数。
想问的问题是,java这样增加参数是否正确,还是需要其他方法。
...全文
74 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
MiceRice 2012-06-13
这样增加参数是可以的,不过有两个问题:
1、漏了开启动作;
2、.Net端,用的是POST方式接收参数还是GET方式?

String urlString = "http://192.168.1.60/upload/Default.aspx?id=1234567890";
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection()
httpConn.setRequestMethod("GET"); // 访问方式
httpConn.connect(); // 关键是这个动作别漏了
回复
岁月之梦 2012-06-13
httpclient 例子很多哦 到网上看看
回复
a395885670 2012-06-13

InputStream inputStream = null;
BufferedReader in = null;
String path = null;
try {
path = ROOT_PATH
+ URLEncoder.encode(MESSAGE + smsContent + PHONE
+ phoneNumber + key, "utf-8");
URL url;
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "text/html");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setDoInput(true);
inputStream = conn.getInputStream();
String line;
in = new BufferedReader(new InputStreamReader(inputStream), 1024);
while ((line = in.readLine()) != null) {
if (line.length() > 0) {
Gson gson = new Gson();
Type type = new TypeToken<SMSResult>() {
}.getType();
smsResult = gson.fromJson(line, type);
}
}
} catch (Exception e) {
throw e;
} finally {
try {
if (null != in) {
in.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (Exception e) {
throw e;
}
}



楼主可以参考下 需要用POST
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2012-06-13 09:09
社区公告
暂无公告