本文整理了Java中java.net.URLConnection.setRequestProperty()
方法的一些代码示例,展示了URLConnection.setRequestProperty()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.setRequestProperty()
方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:setRequestProperty
[英]Sets the value of the specified request header field. The value will only be used by the current URLConnection instance. This method can only be called before the connection is established.
[中]设置指定请求标头字段的值。该值将仅由当前URLConnection实例使用。只能在建立连接之前调用此方法。
代码示例来源:origin: stackoverflow.com
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
InputStream response = connection.getInputStream();
// ...
代码示例来源:origin: stackoverflow.com
public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e(LOG_TAG, "Error checking internet connection", e);
}
} else {
Log.d(LOG_TAG, "No network available!");
}
return false;
}
代码示例来源:origin: wildfly/wildfly
private InputStream streamOpener() throws IOException {
final URL url = configurationUri.toURL();
final URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept", "application/xml,text/xml,application/xhtml+xml");
return connection.getInputStream();
}
代码示例来源:origin: stackoverflow.com
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
FileBody fileBody = new FileBody(new File(fileName));
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
multipartEntity.addPart("file", fileBody);
connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue());
OutputStream out = connection.getOutputStream();
try {
multipartEntity.writeTo(out);
} finally {
out.close();
}
int status = connection.getResponseCode();
...
代码示例来源:origin: stackoverflow.com
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
// ...
代码示例来源:origin: stackoverflow.com
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
String encoded = Base64.encode(username+":"+password);
connection.setRequestProperty("Authorization", "Basic "+encoded);
代码示例来源:origin: geoserver/geoserver
URLConnection conn = url.openConnection();
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
return new GZIPInputStream(conn.getInputStream());
} else if (encoding.equalsIgnoreCase("deflate")) {
return new InflaterInputStream(conn.getInputStream(), new Inflater(true));
} else {
return conn.getInputStream();
代码示例来源:origin: geotools/geotools
/**
* @see org.geotools.data.ows.HTTPClient#post(java.net.URL, java.io.InputStream,
* java.lang.String)
*/
public HTTPResponse post(
final URL url, final InputStream postContent, final String postContentType)
throws IOException {
URLConnection connection = openConnection(url);
if (connection instanceof HttpURLConnection) {
((HttpURLConnection) connection).setRequestMethod("POST");
}
connection.setDoOutput(true);
if (postContentType != null) {
connection.setRequestProperty("Content-type", postContentType);
}
connection.connect();
OutputStream outputStream = connection.getOutputStream();
try {
byte[] buff = new byte[512];
int count;
while ((count = postContent.read(buff)) > -1) {
outputStream.write(buff, 0, count);
}
} finally {
outputStream.flush();
outputStream.close();
}
return new SimpleHTTPResponse(connection);
}
代码示例来源:origin: stackoverflow.com
HttpURLConnection httpUrlConnection = null;
URL url = new URL("http://example.com/server.cgi");
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
httpUrlConnection.setRequestProperty(
"Content-Type", "multipart/form-data;boundary=" + this.boundary);
代码示例来源:origin: stackoverflow.com
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class TestUrlOpener {
public static void main(String[] args) throws IOException {
URL url = new URL("http://localhost:8080/foobar");
URLConnection hc = url.openConnection();
hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
System.out.println(hc.getContentType());
}
}
代码示例来源:origin: wildfly/wildfly
static ConfigurationXMLStreamReader openUri(final URI uri, final XMLInputFactory xmlInputFactory) throws ConfigXMLParseException {
try {
final URL url = uri.toURL();
final URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept", "application/xml,text/xml,application/xhtml+xml");
final InputStream inputStream = connection.getInputStream();
try {
return openUri(uri, xmlInputFactory, inputStream);
} catch (final Throwable t) {
try {
inputStream.close();
} catch (Throwable t2) {
t.addSuppressed(t2);
}
throw t;
}
} catch (MalformedURLException e) {
throw msg.invalidUrl(new XMLLocation(uri), e);
} catch (IOException e) {
throw msg.failedToReadInput(new XMLLocation(uri), e);
}
}
代码示例来源:origin: stackoverflow.com
URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
myURLConnection.setRequestProperty ("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
代码示例来源:origin: stackoverflow.com
URL url = new URL("http://www.example.com/comment");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization",
"Basic "+codec.encodeBase64String(("username:password").getBytes());
代码示例来源:origin: org.codehaus.groovy/groovy
final URLConnection connection = url.openConnection();
if (parameters != null) {
if (parameters.containsKey("connectTimeout")) {
Map<String, CharSequence> properties = (Map<String, CharSequence>) parameters.get("requestProperties");
for (Map.Entry<String, CharSequence> entry : properties.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue().toString());
return connection.getInputStream();
代码示例来源:origin: stackoverflow.com
String urlParameters = "param1=a¶m2=b¶m3=c";
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
String request = "http://example.com/index.php";
URL url = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
wr.write( postData );
}
代码示例来源:origin: stackoverflow.com
public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e(LOG_TAG, "Error checking internet connection", e);
}
} else {
Log.d(LOG_TAG, "No network available!");
}
return false;
}
代码示例来源:origin: stackoverflow.com
URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
代码示例来源:origin: stackoverflow.com
public static boolean hasInternetAccess(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.e(TAG, "Error checking internet connection", e);
}
} else {
Log.d(TAG, "No network available!");
}
return false;
}
代码示例来源:origin: stackoverflow.com
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
代码示例来源:origin: stackoverflow.com
URL resourceUrl, base, next;
HttpURLConnection conn;
String location;
...
while (true)
{
resourceUrl = new URL(url);
conn = (HttpURLConnection) resourceUrl.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections
conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
switch (conn.getResponseCode())
{
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
location = conn.getHeaderField("Location");
base = new URL(url);
next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
continue;
}
break;
}
is = conn.openStream();
...
内容来源于网络,如有侵权,请联系作者删除!