
下面提供二种方法会使用java发送url请求,并获取服务器返回的值
第一种方法:
代码如下:
import orgapache>
publicstaticStringsendUrlRequest(StringurlStr,Stringparam1,Stringparam2)throwsException{
StringtempStr=null;
>
//设置超时时间
>
//封装需要传递的参数
List<NameValuePair>nvps=newArrayList<NameValuePair>();
nvpsadd(newBasicNameValuePair("mainMemoCode",strmainMemoCode));
nvpsadd(newBasicNameValuePair("recordPassWord",strrecordPassWord));
//客户端的请求方法类型
>
//获取服务器返回>
//获取服务器返回页面的值
entity=responsegetEntity();
xmlContent=EntityUtilstoString(entity);
Stringstrmessage=null;
Systemoutprintln(xmlContent);
Systemoutprintln(responsegetHeaders("Content-Type")[0]getValue()toString());
>
}
catch(SocketTimeoutExceptione)
{
}
catch(Exceptionex)
{
exprintStackTrace();
}
finally{
>
代码如下:
publicstaticStringsendUrlRequest(StringurlStr,Stringparam1,Stringparam2)throwsException{
>
PrintWriterout=newPrintWriter(newOutputStreamWriter(connectiongetOutputStream(),"GBK"));
outprintln(param);
outclose();
BufferedReaderin=newBufferedReader(newInputStreamReader(connection
getInputStream(),"GBK"));
StringinputLine;
while((inputLine=inreadLine())!=null){
bankXmlBufferappend(inputLine);
}
inclose();
tempStr=bankXmlBuffertoString();
}
catch(Exceptione)
{
Systemoutprintln("发送GET请求出现异常!"+e);
eprintStackTrace();
}finally{
if(url_con!=null)
url_condisconnect();
}
returntmpeStr;
}
总结:多练习代码,熟练之后才能更快速的去了解代码的学习的方法。多去获取一些思维方面的书籍可以看看。
以上就是关于为什么android httpURLConnection get得到的不是页面源码全部的内容,包括:为什么android httpURLConnection get得到的不是页面源码、如何通过抓包工具fiddler获取java程序的http请求、HttpClient 怎么获取连接的内容长度 response.getEntity().getContentLength() 拿出来的是-1,高手指等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)