Skip to content

Commit

Permalink
HTTP : add setUserAgent()
Browse files Browse the repository at this point in the history
  • Loading branch information
pquiring committed Dec 9, 2024
1 parent b95e3c1 commit 7d671ed
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/javaforce/HTTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class HTTP {
public static boolean debug = false;
private Progress progress;
private static int timeout = 30000;
private static String user_agent = "JavaForce.HTTP";

//form types : see RFC 1867, 7568
/** Form Type for URL encoded data. */
Expand Down Expand Up @@ -329,10 +330,16 @@ public int getCode() {
return code;
}

/** Set Accept header value. */
public void setAccept(String accept) {
this.accept = accept;
}

/** Set User-Agent header value. */
public static void setUserAgent(String ua) {
user_agent = ua;
}

private int read(Buffer buf, int length) throws Exception {
if (buf.count >= length) return length;
int maxlength = buf.buf.length - buf.pos - buf.count;
Expand Down Expand Up @@ -574,7 +581,7 @@ public boolean get(String url, OutputStream os) {
StringBuilder req = new StringBuilder();
req.append("GET " + url + " HTTP/1.1\r\n");
req.append("Host: " + host + (port != 80 ? (":" + port) : "") + "\r\n");
req.append("User-Agent: JavaForce.HTTP\r\n");
req.append("User-Agent: " + user_agent + "\r\n");
req.append("Content-Length: 0\r\n");
req.append("Accept: " + accept + "\r\n");
req.append("Accept-Encoding: chunked\r\n");
Expand Down Expand Up @@ -644,7 +651,7 @@ public boolean post(String url, Part[] parts, OutputStream os) {
StringBuilder req = new StringBuilder();
req.append("POST " + url + " HTTP/1.1\r\n");
req.append("Host: " + host + (port != 80 ? (":" + port) : "") + "\r\n");
req.append("User-Agent: JavaForce.HTTP\r\n");
req.append("User-Agent: " + user_agent + "\r\n");
req.append("Accept: " + accept + "\r\n");
req.append("Accept-Encoding: chunked\r\n");
appendHeaders(req);
Expand Down Expand Up @@ -712,7 +719,7 @@ public boolean post(String url, byte[] data, String mimeType, OutputStream os) {
StringBuilder req = new StringBuilder();
req.append("POST " + url + " HTTP/1.1\r\n");
req.append("Host: " + host + (port != 80 ? (":" + port) : "") + "\r\n");
req.append("User-Agent: JavaForce.HTTP\r\n");
req.append("User-Agent: " + user_agent + "\r\n");
req.append("Accept: " + accept + "\r\n");
req.append("Accept-Encoding: chunked\r\n");
appendHeaders(req);
Expand Down

0 comments on commit 7d671ed

Please sign in to comment.