httpurlconnection 如何发送字符串给服务器
1个回答
推荐于2016-05-31
展开全部
使用HttpURLConnection将字符流发送到servlet
此功能将字符串以流的形式发送给一个servlet.(ps:可用于ajax跨域访问)
代码如下:
java 代码
--------------------
public String postStringToUrl(String Str, String urlStr){
BufferedWriter bWriter = null;
HttpURLConnection urlConn = null;
String message = Str;
String target = urlStr;
String sCurrentLine = '';
String sTotalString = '';
int res = 0;
try{
byte[] byteBuffer = message.getBytes('GB2312');
URL httpurl = new URL(target);
urlConn = (HttpURLConnection)httpurl.openConnection();
urlConn.setRequestProperty('Content-Type','application/octet-stream');
urlConn.setRequestProperty('Content-length', '' + byteBuffer.length);
urlConn.setRequestProperty('pure-data', 'yes');
urlConn.setRequestProperty('Connection','Keep-Alive');
urlConn.setDoOutput(true);
OutputStream out = urlConn.getOutputStream();
try{
out.write(byteBuffer);
}
finally{
out.flush();
out.close();
message = null;
target = null;
}
//url请求返回code值
res = urlConn.getResponseCode();
if (res == 200) {
java.io.InputStream is = urlConn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((sCurrentLine = reader.readLine()) != null)
if (sCurrentLine.length() > 0)
sTotalString = sTotalString + sCurrentLine.trim();
String tmpStr = new String(sTotalString.getBytes('GB2312'));
sTotalString = tmpStr;
} else {
sTotalString = '远程服务器连接失败,错误代码:'+res;
}
if (bWriter != null)
bWriter.close();
}
catch(Exception e){
sTotalString = '连接服务器失败.';
e.printStackTrace();
}
return sTotalString;
}
此功能将字符串以流的形式发送给一个servlet.(ps:可用于ajax跨域访问)
代码如下:
java 代码
--------------------
public String postStringToUrl(String Str, String urlStr){
BufferedWriter bWriter = null;
HttpURLConnection urlConn = null;
String message = Str;
String target = urlStr;
String sCurrentLine = '';
String sTotalString = '';
int res = 0;
try{
byte[] byteBuffer = message.getBytes('GB2312');
URL httpurl = new URL(target);
urlConn = (HttpURLConnection)httpurl.openConnection();
urlConn.setRequestProperty('Content-Type','application/octet-stream');
urlConn.setRequestProperty('Content-length', '' + byteBuffer.length);
urlConn.setRequestProperty('pure-data', 'yes');
urlConn.setRequestProperty('Connection','Keep-Alive');
urlConn.setDoOutput(true);
OutputStream out = urlConn.getOutputStream();
try{
out.write(byteBuffer);
}
finally{
out.flush();
out.close();
message = null;
target = null;
}
//url请求返回code值
res = urlConn.getResponseCode();
if (res == 200) {
java.io.InputStream is = urlConn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((sCurrentLine = reader.readLine()) != null)
if (sCurrentLine.length() > 0)
sTotalString = sTotalString + sCurrentLine.trim();
String tmpStr = new String(sTotalString.getBytes('GB2312'));
sTotalString = tmpStr;
} else {
sTotalString = '远程服务器连接失败,错误代码:'+res;
}
if (bWriter != null)
bWriter.close();
}
catch(Exception e){
sTotalString = '连接服务器失败.';
e.printStackTrace();
}
return sTotalString;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询