1

我需要将我的字符串发布到具有 html 内容的服务器,

这是我的代码

网址生成器

Uri.Builder builder = new Uri.Builder()
    .scheme("http")
    .authority(domain)
    .path(pageName)
    .appendQueryParameter("myaction", "dopost")
    .appendQueryParameter("mesg", msg.replace("\n","<br />"));

生成的网址看起来像

domainname/pagename?myaction=dopost&mesg=Hello%2C%3Cbr%20%2F%3E%3Cbr%20%2F%3ETesting

执行http代码

HttpGet getRequest = new HttpGet(url);
getRequest.setHeader(CONTENT_TYPE, MIME_TEXT_HTML); // html/text, utf-8
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(getRequest);

但这不会在执行后发布得到禁止的错误

预先感谢

4

1 回答 1

0

也许是一个愚蠢的错误,但帖子通常用 anHttpPost而不是 an完成HttpGet ,看起来像这样:

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);    
}
于 2013-02-14T11:38:51.213 回答