java開発でHttpClientクラスでHttpPostを利用するプログラム

コード下記
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HttpostStartnews24 {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(“xxx/cft");
List<NameValuePair> npr = new ArrayList<NameValuePair>();
npr.add(new BasicNameValuePair(“username", “startnews24user"));
npr.add(new BasicNameValuePair(“password", “startnews24pwd"));
httpPost.setEntity(new UrlEncodedFormEntity(npr));
HttpResponse response = httpclient.execute(httpPost);
httpclient.getConnectionManager().shutdown();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(response.getEntity());
System.out.println(strResult);
}
}
}

Java

Posted by arkgame