android HttpsURLConnection.setphp request methodMethod("POST")不成功,仍然是GET方法!

在我们之前学习Java的网络编程当中,我们已经见过HttpUrlConnection类的使用。HttpUrlConnect在Java是一个支持http特定的功能一个类,在许多的网络编程经常使用到它。今天我在这里记录的是android中使用HttpUrlConnection,之前我还学过使用异步加载来加载一个网站,它使用的原理同样是多线程,但是它使用的是用json来解析一个网站,而不是直接使用域名也就是网址来解析一个网站,而将要介绍的HttpUrlConnection是使用域名来解析一个网站的,当然它同时使用到了handler类,因为这个事例使用到了子线程来加载ui,主线程来更新ui。代码如下
1 &LinearLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android_http.MainActivity" &
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
13 &/LinearLayout&
从上面的xml中可以看出,我们只用了一个控件,那就是WebView。webview在加载网站当中自有它自己的妙处。
1.首先是Mainactivity类
1 package com.example.android_
3 import android.app.A
4 import android.os.B
5 import android.os.H
6 import android.webkit.WebV
8 public class MainActivity extends Activity {
private WebView webview = null;
private Handler handler = new Handler();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webview);
new HttpThread("", webview, handler).start();
2.其次是HttpThread类,这个继承类Thread类,创建了一个子线程来加载网页
1 package com.example.android_
3 import java.io.BufferedR
4 import java.io.IOE
5 import java.io.InputStreamR
6 import java.net.HttpURLC
7 import java.net.MalformedURLE
8 import java.net.ProtocolE
9 import java.net.URL;
11 import javax.net.ssl.HttpsURLC
13 import android.os.H
14 import android.webkit.WebV
16 public class HttpThread
extends Thread{
private String url = null;
private WebView webview = null;
private Handler handler = null;
public HttpThread(String url, WebView webview, Handler handler)
this.handler =
this.url =
this.webview =
public void run() {
* 关于HttpUrlConnection的使用,有什么不懂,请回去认真复习一下Java的网络编程
HttpURLConnection httpurlconnection = null;
URL url = new URL(this.url);
httpurlconnection = (HttpURLConnection) url.openConnection();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//允许超时5秒
httpurlconnection.setReadTimeout(5000);
//以get的方式与服务器相连接
httpurlconnection.setRequestMethod("GET");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
final StringBuffer sb = new StringBuffer();
String str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(httpurlconnection.getInputStream()));
while((str = br.readLine()) != null)
sb.append(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
handler.post(new Runnable() {
public void run() {
//第一个参数表示的是需要加载的数据--这个先前已经在输入流读入到了sb中
//第二参数表示的是需要加载网站的编码格式--百度的编码格式就是如此
* 需要注意的是:
* loaddata不加载图片,如果想要加载图片,请使用loadDataWithBaseURL
webview.loadData(sb.toString(), "text/charset=utf-8", null);
阅读(...) 评论()trackbacks-0
1.HttpURLConnection连接URL 1)创建一个URL对象
URL url = new URL();
2)利用HttpURLConnection对象从网络中获取网页数据
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3)设置连接超时
conn.setConnectTimeout(6*1000);
4)对响应码进行判断
if (conn.getResponseCode() != 200) &&&//从Internet获取网页,发送请求,将网页以流的形式读回来
throw new RuntimeException("请求url失败");
5)得到网络返回的输入流
InputStream is = conn.getInputStream(); 6)String result = readData(is, "GBK"); //文件流输入出文件用outStream.write 7)conn.disconnect();
总结: --记得设置连接超时,如果网络不好,Android系统在超过默认时间会收回资源中断操作. --返回的响应码200,是成功. --在Android中对文件流的操作和JAVA SE上面是一样的. --在对大文件的操作时,要将文件写到SDCard上面,不要直接写到手机内存上. --操作大文件是,要一遍从网络上读,一遍要往SDCard上面写,减少手机内存的使用.这点很重要,面试经常会被问到. --对文件流操作完,要记得及时关闭.
2.向Internet发送请求参数 步骤: 1)创建URL对象:URL realUrl = new URL(requestUrl); 2)通过HttpURLConnection对象,向网络地址发送请求
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); 3)设置容许输出:conn.setDoOutput(true); 4)设置不使用缓存:conn.setUseCaches(false); 5)设置使用POST的方式发送:conn.setRequestMethod("POST"); 6)设置维持长连接:conn.setRequestProperty("Connection", "Keep-Alive"); 7)设置文件字符集:conn.setRequestProperty("Charset", "UTF-8"); 8)设置文件长度:conn.setRequestProperty("Content-Length", String.valueOf(data.length)); 9)设置文件类型:conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 10)以流的方式输出. 总结: --发送POST请求必须设置允许输出 --不要使用缓存,容易出现问题. --在开始用HttpURLConnection对象的setRequestProperty()设置,就是生成HTML文件头.
3.向Internet发送xml数据 XML格式是通信的标准语言,Android系统也可以通过发送XML文件传输数据. 1)将生成的XML文件写入到byte数组中,并设置为UTF-8:byte[] xmlbyte = xml.toString().getBytes("UTF-8"); 2)创建URL对象,并指定地址和参数:URL url = new URL(); 3)获得链接:HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 4)设置连接超时:conn.setConnectTimeout(6* 1000); 5)设置允许输出conn.setDoOutput(true); 6)设置不使用缓存:conn.setUseCaches(false); 7)设置以POST方式传输:conn.setRequestMethod("POST");&&&&&&&&&&& 8)维持长连接:conn.setRequestProperty("Connection", "Keep-Alive"); 9)设置字符集:conn.setRequestProperty("Charset", "UTF-8"); 10)设置文件的总长度:conn.setRequestProperty("Content-Length", String.valueOf(xmlbyte.length)); 11)设置文件类型:conn.setRequestProperty("Content-Type", "text/ charset=UTF-8"); 12)以文件流的方式发送xml数据:outStream.write(xmlbyte); 总结: --我们使用的是用HTML的方式传输文件,这个方式只能传输一般在5M一下的文件. --传输大文件不适合用HTML的方式,传输大文件我们要面向Socket编程.确保程序的稳定性 --将地址和参数存到byte数组中:byte[] data = params.toString().getBytes();
阅读(...) 评论()HTTP or HTTPS POST in Android using HTTPURLConnection | Digital Library World
My Work and Things I Can't Find Googling!
I’m starting to dive deep into Android world. The first step is usually the hardest. I’m starting to grasp how the Android environment operates. The best way for me to learn is by reading, and actually doing it. So, I started to build a room reservation app using an web service API built by my co-worker. Calling this API requires HTTP POST call from Android. This is where it gets interesting. I tried scouring the web for hours, and all the example I found did not work! I finally looked into , which is really where I should started first. It seems so simple. I could do it in 10 seconds in c#. However during my trial, all the post parameters are not recognized by the API, which is written using PHP. I’ll describe some of the mistake I made.
//import these on your header
import java.io.IOE
import java.io.PrintW
import java.net.HttpURLC
import java.net.MalformedURLE
import java.net.ProtocolE
import java.net.URL;
import java.net.URLE
import java.util.S
//do this wherever you are wanting to POST
//if you are using https, make sure to import java.net.HttpsURLConnection
url=new URL(“http://somesite/somefile.php”);
//you need to encode ONLY the values of the parameters
String param=”param1=” + URLEncoder.encode(“value1″,”UTF-8″)+
“&param2=”+URLEncoder.encode(“value2″,”UTF-8″)+
“&param3=”+URLEncoder.encode(“value3″,”UTF-8″);
conn=(HttpURLConnection)url.openConnection();
//set the output to true, indicating you are outputting(uploading) POST data
conn.setDoOutput(true);
//once you set the output to true, you don’t really need to set the request method to post, but I’m doing it anyway
conn.setRequestMethod(“POST”);
//Android documentation suggested that you set the length of the data you are sending to the server, BUT
// do NOT specify this length in the header by using conn.setRequestProperty(“Content-Length”, length);
//use this instead.
conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);
//send the POST out
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();
//build the string to store the response text from the server
String response= “”;
//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());
//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());
//catch some error
catch(MalformedURLException ex){
Toast.makeText(MyActivity.this, ex.toString(), 1 ).show();
// and some more
catch(IOException ex){
Toast.makeText(MyActivity.this, ex.toString(), 1 ).show();
That’s it. Whatever output you receive from the URL you are calling would be stored in response string.
CAPTCHA Code *
Categories
Select Category
Android&&(5)
Apache Pig&&(1)
Classic ASP&&(1)
CONTENTdm&&(2)
data visualization&&(1)
debian&&(1)
Google App Engine&&(1)
Hadoop&&(2)
HYPER-V Virtualization&&(1)
IIS7 Windows 2008&&(2)
IIS7.5&&(3)
IPSec&&(1)
iSCSI&&(1)
Linux&&(2)
Machine Learning&&(1)
nagios&&(1)
PowerConnect 5424&&(1)
PowerShell&&(1)
python&&(2)
Switch Configuration&&(1)
System Center DPM&&(1)
Uncategorized&&(1)
Windows 2008 R2 Server&&(8)
Windows 2008 Server&&(7)
September 2016
12131415161718
19202122232425
2627282930<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&}

我要回帖

更多关于 requestmethod.put 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信