我有一种感觉,我在这里做错了,但我不太确定我是否错过了一步,或者只是遇到了编码问题.这是我的代码:
URL url = new URL("http://api.stackoverflow.com/0.8/questions/2886661");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
// Question q = new Gson().fromJson(in, Question.class);
String line;
StringBuffer content = new StringBuffer();
while ((line = in.readLine()) != null)
{
content.append(line);
}
Run Code Online (Sandbox Code Playgroud)
当我打印内容时,我得到了一大堆的翅膀和特殊的角色,基本上是乱七八糟的.我会在这里复制并通过它,但这不起作用.我究竟做错了什么?
我正在使用套接字来创建给定服务器的POST请求.响应大部分都回来了,我正在使用编码为"UTF-8"的InputStream来读取服务器的响应.大多数响应是有道理的,我能够正确地查看HTML,但是,看似随机,我看到"1ffa","6e8","1972","90","0"等代码出现我正在读它的响应中的单行.这是我创建和读取响应的方式.
String hostname = "server";
SocketFactory socketFactory = SSLSocketFactory.getDefault();
Socket socket = new Socket(hostname, 8080);
// Create streams to securely send and receive data to the server
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
PrintWriter writer = new PrintWriter(out);
writer.println("POST /handlerServlet http/1.1");
writer.println("Host: " + hostname);
String parameters="params=" + URLEncoder.encode("paramsToEncode", "UTF-8");
writer.println("Content-Length: " + parameters.length());
writer.println("Content-Type: application/x-www-form-urlencoded");
writer.println("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
writer.println("Keep-Alive: 115");
writer.println("Connection: keep-alive");
writer.println("\r\n" + parameters + "\r\n");
writer.flush();
// Read from in and write to out...
String input …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何在java中发送HTTP请求?
如何使用java.net.URLConnection来触发和处理HTTP请求?
使用java,我怎么打任何网址?
例如,在浏览器中打开http://www.xyz.com/node1会告诉xyz.com node1被命中.所以在这个java程序中(它发送sms文本,将上面示例中的'node1'嵌入到url本身的sms网关服务器中)
如何在不打开任何浏览器或使用servlet的情况下实现它.
我[目前正在使用这样的东西
HttpURLConnection con = (HttpURLConnection) u.openConnection ();
con.setDoInput(true);
con.setRequestMethod("POST");
con.setDoInput (true);
con.setDoOutput (true);
con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
out = new DataOutputStream(con.getOutputStream());
String content = "username=" + URLEncoder.encode ("bob")
+ "&password=" + URLEncoder.encode ("smith");
System.out.println("\n" + "sending form to HTTP server ...");
out.writeBytes (content);
out.flush ();
out.close ();
con.connect();
Run Code Online (Sandbox Code Playgroud)
有了这个,我设法将一些数据传递给我的服务器.我现在正在徘徊的是这样可以发送多少钱?
我希望能够发送一些xml文件(100~200行),想知道我能不能这样做?
贾森
我正在尝试编写一个Android应用程序,它将拍摄照片,将数据(byte [])与一些元数据放在一个对象中,并将其发布到AppEngine服务器,在那里它将作为blob保存在数据存储区中.我真的不想将图像保存为Android上的文件(除非绝对必要).我四处寻找解决方案,但没有任何明确或具体的解决方案.我的问题是:
代码示例非常有用.此外,如果我采取的方法太复杂或有问题,请建议其他方法(例如将图像保存为文件,发布到服务器,然后删除).
我需要从servlet访问会话范围的bean.我已经试过了
UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");
Run Code Online (Sandbox Code Playgroud)
如在此描述的职位.但是我只得到null作为结果,尽管UserBean的实例是alreay instatiated.这些是我用于userBean的注释/导入:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
... }
Run Code Online (Sandbox Code Playgroud)
一些背景为什么我无法摆脱servlet:我的jsf页面中有一个文件上传小程序.这个小程序需要一个地址,它可以发送它的POST请求.(我无法编辑此帖子请求以添加更多字段或其他内容).然后我的servlet的post方法存储文件.托管bean无法完成此作业,因为必须使用@MultiPartConfig注释servlet,并且我无法将此批注添加到jsf托管bean.
我有一个类有两个方法,有很多重复的代码但是唯一的位在整个事件的中间.根据我的研究,我认为我应该做"执行周围方法"模式,但我找不到我可以遵循的资源,因为他们似乎都使用我无法复制的代码.
我有两种方法,apiPost和apiGet,我已粘贴在下面.我已经将这些方法的独特部分包装在一起,注释显示了唯一部分的开始和结束位置:
/**
* Class that handles authorising the connection and handles posting and getting data
*
* @version %I%, %G%
* @since 1.0
*/
public class CallHandler {
private static PropertyLoader props = PropertyLoader.getInstance();
final static int MAX = props.getPropertyAsInteger(props.MAX_REQUESTS);
private final Logger log = LoggerFactory.getLogger(CallHandler.class);
private final static String POST = "POST";
private final static String GET = "GET";
/**
* Makes a POST call to the API URL provided and returns the JSON response as a string …Run Code Online (Sandbox Code Playgroud) 我使用java.net在我的Java客户端发送HTTP请求,但我仍然无法实现/找到如何实际触发请求.
例如,我有这个代码:
Scanner sc = new Scanner(System.in);
System.out.println("Deleting subject...");
System.out.println("Subject shortcut (-1 for return):");
String shortcut = sc.next();
if( shortcut.equals("-1") )
return ;
try
{
URL url = new URL( "http://localhost:8080/Server/webresources/subject/delete/"+shortcut );
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("DELETE");
BufferedReader br = new BufferedReader( new InputStreamReader( con.getInputStream() ) );
System.out.println( br.readLine() );
}catch( Exception e )
{
System.out.println(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,如果我不使用这些行:
BufferedReader br = new BufferedReader( new InputStreamReader( con.getInputStream() ) );
System.out.println( br.readLine() );
Run Code Online (Sandbox Code Playgroud)
请求永远不会发送.因此,在这种情况下,请求似乎是通过从连接调用InputStream来触发的.
任何人都可以解释我如何通过java.net触发HTTP请求?
我用jsoup从不同页面的html源代码中提取一些信息.其中大多数是UTF-8编码的.其中一个用ISO-8859-1编码,这导致了一个奇怪的错误(在我的选择中).
包含错误的页面是:http: //www.gudi.ch/armbanduhr-metall-wasserdicht-1280x960-megapixels-p-560.html
我用以下代码读取了所需的String:
Document doc = Jsoup.connect("http://www.gudi.ch/armbanduhr-metall-wasserdicht-1280x960-megapixels-p-560.html").userAgent("Mozilla").get();
String title = doc.getElementsByClass("products_name").first().text();
Run Code Online (Sandbox Code Playgroud)
问题是字符串"HD Armbanduhr aus Metall 4GB Wasserdicht 1280X960 - 5百万像素"中的连字符.像öäü这样的正常变音符号被正确读取.只有这个单一字符,没有输出为"-" 出现问题.
我试图用out.outputSettings().charset("ISO-8859-1")覆盖(正确设置)页面编码,但这也没有帮助.
接下来,我尝试使用Charset类将字符串的编码从手动更改为utf8和iso-8859-1.也没有运气.
在使用jsoup解析html文档后,有人提示我可以尝试获取正确的字符吗?
谢谢
我必须将文件上传到服务器,该服务器只显示带有文件上传按钮的jsf网页(通过http).我必须自动化一个进程(作为java独立进程完成),它生成一个文件并将文件上传到服务器.然后,必须上传文件的服务器不提供FTP或SFTP.有没有办法做到这一点?
谢谢,里奇
java ×9
http ×2
jsf ×2
servlets ×2
url ×2
android ×1
applet ×1
file-upload ×1
html ×1
html-parsing ×1
httprequest ×1
httpresponse ×1
json ×1
jsoup ×1
managed-bean ×1
sockets ×1
xml ×1